Android Toolbar actionLayout of Switch causes text to jump when menu is pressed

不问归期 提交于 2019-12-01 00:33:22

I was following your code to add Switch to my action bar and I was able to solve your problem with the "jumping text" by setting the width of the Switch to wrap_content like this:

<Switch xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/action_switch"
    android:text="@string/action_switch"
    android:switchPadding="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>

I gave up trying to use the built in Text field of a Switch or SwitchCompat control. I ended up providing a layout with a separate TextView field:

layout/toolbar_switch.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/action_switch_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingEnd="10dp"
        android:paddingRight="10dp"
        android:text="Switch"
        android:textAllCaps="true"
        tools:ignore="HardcodedText,RtlHardcoded,RtlSymmetry"/>

    <android.support.v7.widget.SwitchCompat
        android:id="@+id/action_switch_control"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!