本帖最后由 liu 于 2018-12-14 12:06 编辑
自定义Toast
为了更好看的提示效果所以就要自定义Toast了,直接上代码:
activity_main.xml(就一个按钮用来点击弹出toast):
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击显示Toast" />
</LinearLayout>
自定义的Toast布局:
res/layout/toast.xml:
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:background="@drawable/toast"
android:layout_marginRight="15dp"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="45dp"
android:scaleType="fitXY"
android:src="@drawable/icon_header" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:padding="15dp">
<ImageView
android:id="@+id/iv_logo"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/sflogo" />
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:textColor="@android:color/white" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
res/drawable/toast:
[XML] 纯文本查看 复制代码 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp" />
<stroke
android:width="1dp"
android:color="@color/colorAccent" />
<solid android:color="@android:color/black" />
</shape>
MainActivity:
[Java] 纯文本查看 复制代码 package com.company.helloworld.firstapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.btn);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showToast();
}
});
}
private void showToast() {
LayoutInflater inflater = getLayoutInflater();
//加载自定义的布局
View view = inflater.inflate(R.layout.toast, null);
ImageView iv_logo = view.findViewById(R.id.iv_logo);
iv_logo.setImageResource(R.drawable.sflogo);
TextView tv_content = view.findViewById(R.id.tv_content);
tv_content.setText("http://www.sufeinet.com/forum-183-1.html");
Toast toast = new Toast(this);
//设置Toast显示的位置
toast.setGravity(Gravity.CENTER, 0, 0);
//设置持续时间
toast.setDuration(Toast.LENGTH_LONG);
//添加视图文件
toast.setView(view);
toast.show();
mButton.setVisibility(View.GONE);
}
}
效果图:
|