在Android菜单图标中的开关按钮下方/上方添加文本

编程入门 行业动态 更新时间:2024-10-24 22:25:38
本文介绍了在Android菜单图标中的开关按钮下方/上方添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在 Toolbar 中此 switch 项下方/上方添加文本,以指示该开关的功能.我想知道是否可以使用布局标签在按钮下方/上方添加文本.还是我必须使用其他方法.

menu_main.xml

<菜单xmlns:android ="schemas.android/apk/res/android"xmlns:app =" schemas.android/apk/res-autoxmlns:tools ="schemas.android/tools"工具:context =.MainActivity".<项目android:id ="@@ id/edit";android:title =" @ string/app_name"app:actionViewClass =" android.widget.Switch"android:icon ="@ drawable/ic_edit"app:showAsAction =始终"android:visible ="true"/>

当前看起来像这样:

解决方案

您可以创建一个自定义菜单布局,其中包含其描述的 Switch 和 TextView

layout/menu_layout.xml

<?xml version ="1.0"encoding ="utf-8"?< LinearLayout xmlns:android =" schemas.android/apk/res/android"android:id ="@@ id/menuRoot"android:layout_width =" match_parent"android:layout_height =" match_parent"android:paddingTop =" 8dp"android:gravity ="center"android:orientation =" vertical">< androidx.appcompat.widget.SwitchCompatandroid:id =" @ + id/menu_switch"android:layout_width =" wrap_content"android:layout_height =" wrap_content"/>< TextViewandroid:id =" @ + id/switch_text"android:layout_width =" wrap_content"android:layout_height =" wrap_content"android:text =" OFF"android:textColor =" @ color/white"android:textSize =" 12sp"/></LinearLayout>

然后利用菜单项的 app:actionLayout 属性附加自定义菜单

menu/my_menu.xml

<?xml version ="1.0"encoding ="utf-8"?< menu xmlns:android =" schemas.android/apk/res/android"xmlns:app =" schemas.android/apk/res-auto><项目android:id ="@@ id/switchItem"android:title ="switch"app:actionLayout =" @ layout/menu_layout;app:showAsAction =始终"/></菜单>

您可以检测到开关单击回调并相应地更改说明,如下所示:

@Overridepublic boolean onCreateOptionsMenu(Menu menu){getMenuInflater().inflate(R.menu.my_menu,menu);MenuItem item = menu.findItem(R.id.switchItem);LinearLayout根= item.getActionView().findViewById(R.id.menuRoot);SwitchCompat菜单Switch = root.findViewById(R.id.menu_switch);TextView switchText = root.findViewById(R.id.switch_text);menuSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked){switchText.setText(isChecked?"ON":"OFF");}});返回true;}

预览

I am trying to add a text below/above this switch item that is in my Toolbar to indicate functionality of the switch. I was wondering if there is a layout tag I could use to add a text below/above the button. Or is there some other method I have to use.

menu_main.xml

<menu xmlns:android="schemas.android/apk/res/android" xmlns:app="schemas.android/apk/res-auto" xmlns:tools="schemas.android/tools" tools:context=".MainActivity"> <item android:id="@+id/edit" android:title="@string/app_name" app:actionViewClass="android.widget.Switch" android:icon="@drawable/ic_edit" app:showAsAction="always" android:visible="true"/>

Currently it looks like this:

解决方案

You can create a custom menu layout that has a Switch and a TextView of its description

layout/menu_layout.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="schemas.android/apk/res/android" android:id="@+id/menuRoot" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="8dp" android:gravity="center" android:orientation="vertical"> <androidx.appcompat.widget.SwitchCompat android:id="@+id/menu_switch" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/switch_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="OFF" android:textColor="@color/white" android:textSize="12sp" /> </LinearLayout>

Then utilize the app:actionLayout attribute of the menu item to attach the custom menu

menu/my_menu.xml

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="schemas.android/apk/res/android" xmlns:app="schemas.android/apk/res-auto"> <item android:id="@+id/switchItem" android:title="switch" app:actionLayout="@layout/menu_layout" app:showAsAction="always" /> </menu>

And you can detect the switch click callbacks and change the description accordingly as below:

@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.my_menu, menu); MenuItem item = menu.findItem(R.id.switchItem); LinearLayout root = item.getActionView().findViewById(R.id.menuRoot); SwitchCompat menuSwitch = root.findViewById(R.id.menu_switch); TextView switchText = root.findViewById(R.id.switch_text); menuSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { switchText.setText(isChecked? "ON" : "OFF"); } }); return true; }

A preview

更多推荐

在Android菜单图标中的开关按钮下方/上方添加文本

本文发布于:2023-06-07 21:56:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/568473.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图标   按钮   菜单   文本   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!