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>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端