主要代码
public void showToastInfoShort(String str)
{
this.runOnUiThread(new Runnable()
{
@Override
public void run()
{
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.toastmessage, (ViewGroup) findViewById(R.id.toast_message));
TextView tvToastMessage = view.findViewById(R.id.tvToastMessage);
LogUtil.i(str);
tvToastMessage.setText(str);
toast.setView(view);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();
}
});
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/toast_message"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvToastMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Activity文件
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.butSubmit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
showToastInfoShort("我是自定义弹窗");
}
});
}
圆角显示
1.在drawable中添加xml文件
2.写入一下代码
<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffffff"/>
<corners android:topRightRadius="80px" android:topLeftRadius="80px"/>
</shape>
3.在控件的background中进行引用 @drawable/XXX