BottomSheetDialog 的基本用法
场景:底部弹窗包含几个可选的菜单按钮。
点击页面上的按钮触发
switch (item.getItemId()){ case R.id.item_theme: dialog = new BottomSheetDialog(MainActivity.this); View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.theme_choose,null); //加载 theme_choose的XML文件 dialog.setContentView(view); dialog.show(); theme_choose(view); //调用这个方法,对按钮进行事件绑定,这个view必须穿不然点击事件绑定失败 break; }
接下来看 theme_choose 方法;
//主题样式选择 点击事件绑定 public void theme_choose(View view){ view.findViewById(R.id.theme_blue).setOnClickListener(this); view.findViewById(R.id.theme_black).setOnClickListener(this); view.findViewById(R.id.theme_pink).setOnClickListener(this); view.findViewById(R.id.theme_purple).setOnClickListener(this); view.findViewById(R.id.theme_yellow).setOnClickListener(this); }
点击弹出层里面的菜单可以触发,这里只展示一个分支,其他的分支都是一样改了参数值。
case R.id.theme_purple: //Toast.makeText(MainActivity.this,"theme_purple",Toast.LENGTH_LONG).show(); mtoolbar.setBackgroundColor(Color.parseColor("#6A5ACD")); editor.putString("theme","#6A5ACD"); editor.commit(); break;
注:mtoolbar是toolbar 的导航栏,修改了这个导航栏的背景颜色
<androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_height="wrap_content" android:layout_width="match_parent" android:background="#DF0A0A" />
theme_choose的XML文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="20dp" > <ImageView android:id="@+id/theme_blue" android:layout_height="60dp" android:layout_width="60dp" android:layout_weight="1" android:src="@mipmap/theme_blue" /> <ImageView android:id="@+id/theme_yellow" android:layout_height="60dp" android:layout_width="60dp" android:layout_weight="1" android:src="@mipmap/theme_yellow" /> <ImageView android:id="@+id/theme_pink" android:layout_height="60dp" android:layout_width="60dp" android:layout_weight="1" android:src="@mipmap/themem_pink" /> <ImageView android:id="@+id/theme_black" android:layout_height="60dp" android:layout_width="60dp" android:layout_weight="1" android:src="@mipmap/theme_black" /> <ImageView android:id="@+id/theme_purple" android:layout_height="60dp" android:layout_width="60dp" android:layout_weight="1" android:src="@mipmap/theme_purple" /> </LinearLayout>

浙公网安备 33010602011771号