Adding text below/above a switch button in Android menu icon

浪尽此生 提交于 2021-01-29 04:53:57

问题


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:


回答1:


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




回答2:


It is not necessary. The user can understand. Developers often don't put text into tools, it's the placement of icons. You can learn more about the menu at: https://developer.android.com/guide/topics/ui/menus?hl=vi



来源:https://stackoverflow.com/questions/65174901/adding-text-below-above-a-switch-button-in-android-menu-icon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!