开关Android的工具栏actionLayout导致文本跳的时候,菜单是pressed工具栏、菜单、文本、时候

2023-09-12 21:52:30 作者:挽手说余生

请参考我的项目在: https://github.com/paulpv/ToolbarSwitch

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

我正在为Android工具栏(又名:新的动作条)一个简单的自定义布局。 这个简单的控制增加了一个切换到工具栏,可以在这里看到品红色突出显示:

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:

我的问题是立即当溢出菜单是pressed,交换机的文本跳转到工具栏的顶部,并保持在关闭溢出菜单方式:

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).

我能做些什么,以我的菜单项的actionLayout至prevent文跳楼?

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

菜单/ menu_main.xml

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>

布局/ toolbar_switch.xml

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

很明显,我想我可以创建两个观点,一种TextView的,和一个开关,在这里我分别设置在TextView的文字,我不直接设置交换机的文字...但我会preFER到直接在交换机上设置文本。

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.

我有几个其他问题:

当使用ActionBarActivity和充气工具栏菜单,为什么onResume onCreateOptionsMenu前叫什么名字? 相反,在相同的情况下为#1,为什么onCreateOptionsMenu的onCreate和onResume之间不叫? 在横向模式下,标题文字大小是明显较小;这是预期和理想? 在我能做些什么来设置景观标题文本的大小是一样的肖像文字大小?

谢谢!

值Pv

推荐答案

我是按照你的code加切换到我的行动吧,我能够通过设置宽度为您解决问题的跳跃的文字该开关的WRAP_CONTENT是这样的:

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