问题
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:
![](https://www.eimg.top/images/2020/03/28/a258dcbf546a58828264ad95a03e3286.png)
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:
![](https://www.eimg.top/images/2020/03/28/8b4b6aaae749d2108e9549a2f49df1b4.png)
![](https://www.eimg.top/images/2020/03/28/1178f88c802ee08aeebe2a68156e57b4.png)
A similar thing happens in Landscape orientation (the subtle difference being that the overflow menu popup doesn't have any top padding).
![](https://www.eimg.top/images/2020/03/28/b3d12fa1aada09cb42e7ac9ae25818e2.png)
![](https://www.eimg.top/images/2020/03/28/7f8826ca65ecaf996b1f885ebcab270f.png)
![](https://www.eimg.top/images/2020/03/28/a9abace1ba8206c6a2cbe485d417ce8c.png)
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:
- When using an ActionBarActivity and inflating the Toolbar menu, why is onResume called before onCreateOptionsMenu?
- Conversely, in the same circumstance as #1, why is onCreateOptionsMenu not called between onCreate and onResume?
- In landscape mode, the title text size is noticeably smaller; is this expected and desirable?
- 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