一、步骤
1、引用依赖
implementation 'com.android.support:design:26.1.0'
2、创建Layout布局
bottomdailog.xml
3、创建BottomSheetDialog
BottomSheetDialog dialog = new BottomSheetDialog(this);
4、引入自定View
View dialogView = LayoutInflater.from(this).inflate(R.layout.bottomdailog, null);
5、引用控件
findViewById(R.id.more_login_exception)
6、控件的点击事件处理
.setOnClickListener(this)
7、View添加到BottomSheetDialog
dialog.setContentView(dialogView);
8、显示.show()
dialog.show();
二、完整代码
1、页面布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/more_login_exception" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="10dp" android:text="登录异常" android:textSize="20sp" /> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:background="@color/colorHint" /> <TextView android:id="@+id/more_contact_customer" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="10dp" android:text="联系客服" android:textSize="20sp" /> <View android:layout_width="match_parent" android:layout_height="5dp" android:background="@color/colorHint" /> <TextView android:id="@+id/tv_cancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="10dp" android:text="关闭" android:textSize="20sp" /> </LinearLayout>
2、方法调用
public void showDialog() { final BottomSheetDialog dialog = new BottomSheetDialog(this); View dialogView = LayoutInflater.from(this).inflate(R.layout.bottomdailog, null); TextView more_login_exception = dialogView.findViewById(R.id.more_login_exception); TextView more_contact_customer = dialogView.findViewById(R.id.more_contact_customer); TextView tv_cancel = dialogView.findViewById(R.id.tv_cancel); more_login_exception.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(LoginActivity.this, "登录异常", Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); more_contact_customer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(LoginActivity.this, "联系客服", Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); tv_cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); dialog.setContentView(dialogView); dialog.show(); }