在Android菜单图标中的开关按钮下方/上方添加文本 [英] Adding text below/above a switch button in Android menu icon
问题描述
我正在尝试在 Toolbar
中此 switch
项下方/上方添加文本,以指示该开关的功能.我想知道是否可以使用布局标签在按钮下方/上方添加文本.还是我必须使用其他方法.
menu_main.xml
<菜单xmlns:android ="http://schemas.android.com/apk/res/android"xmlns:app =" http://schemas.android.com/apk/res-autoxmlns:tools ="http://schemas.android.com/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 =" http://schemas.android.com/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 =" http://schemas.android.com/apk/res/android"xmlns:app =" http://schemas.android.com/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="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/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="http://schemas.android.com/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="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/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菜单图标中的开关按钮下方/上方添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!