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

我是研究僧i 提交于 2020-01-21 05:26:05

问题


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 simple control adds a Switch to the Toolbar, and can be seen here highlighted in magenta:

My problem is that immediately when the overflow menu is pressed, the Switch's Text jumps to the top of the Toolbar, and remains that way when you close the overflow menu:

A similar thing happens in Landscape orientation (the subtle difference being that the overflow menu popup doesn't have any top padding).

What can I do to my menu item's actionLayout to prevent the text from jumping?

menu/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/action_switch"
        android:title="@string/action_switch"
        app:actionLayout="@layout/toolbar_switch"
        app:showAsAction="always" />

    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never" />
</menu>

layout/toolbar_switch.xml

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

Obviously, I think I could create two Views, one TextView, and one Switch, where I separately set the text on the TextView and I don't directly set the Switch's text...but I would prefer to set the text directly on the Switch.

I have a few other questions:

  1. When using an ActionBarActivity and inflating the Toolbar menu, why is onResume called before onCreateOptionsMenu?
  2. Conversely, in the same circumstance as #1, why is onCreateOptionsMenu not called between onCreate and onResume?
  3. In landscape mode, the title text size is noticeably smaller; is this expected and desirable?
  4. What can I do to set the landscape title text size to be the same as the portrait text size?

Thanks!

Pv


回答1:


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



回答2:


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>


来源:https://stackoverflow.com/questions/28664412/android-toolbar-actionlayout-of-switch-causes-text-to-jump-when-menu-is-pressed

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