【问题标题】:Adding text below/above a switch button in Android menu icon在 Android 菜单图标中的切换按钮下方/上方添加文本
【发布时间】:2020-12-07 01:20:33
【问题描述】:

我正在尝试在我的Toolbar 中的 switch 项目下方/上方添加文本,以指示开关的功能。我想知道是否有一个布局标签可以用来在按钮下方/上方添加文本。或者我有什么其他方法我必须使用。

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"/>

目前看起来是这样的:

【问题讨论】:

    标签: java android android-layout android-actionbar android-toolbar


    【解决方案1】:

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

    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>
    

    然后利用菜单项的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">
    
        <item
            android:id="@+id/switchItem"
            android:title="switch"
            app:actionLayout="@layout/menu_layout"
            app:showAsAction="always" />
    
    </menu>
    

    您可以检测到开关点击回调并相应地更改描述如下:

        @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;
        }
    

    预览

    【讨论】:

      【解决方案2】:

      没有必要。 用户可以理解。 开发人员通常不会将文本放入工具中,而是放置图标。 您可以通过以下方式了解更多关于菜单的信息:https://developer.android.com/guide/topics/ui/menus?hl=vi

      【讨论】:

      • 我正计划添加更多开关,所以在我自己看来有文字会有所帮助
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      • 2018-01-15
      • 1970-01-01
      相关资源
      最近更新 更多