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

前端 未结 2 1277
鱼传尺愫
鱼传尺愫 2021-01-06 13:35

Please refer to my project at: https://github.com/paulpv/ToolbarSwitch

I am working on a simple custom layout for the Android Toolbar (aka: new ActionBar). This simp

相关标签:
2条回答
  • 2021-01-06 13:57

    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"
    />
    
    0 讨论(0)
  • 2021-01-06 14:13

    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>
    
    0 讨论(0)
提交回复
热议问题