请参阅我的项目:https://github.com/paulpv/ToolbarSwitch
我正在为Android (又名:新ActionBar)做一个简单的定制布局。这个简单的控件向工具栏添加了一个开关,在这里可以在洋红色中高亮显示:

我的问题是,当按下溢出菜单时,开关的文本会跳到工具栏的顶部,关闭溢出菜单时仍然是这样:


一个类似的事情发生在景观导向(微妙的区别是溢出菜单弹出没有任何顶部填充)。



我能对菜单项的actionLayout做些什么来防止文本跳转?
菜单/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>布局/工具栏_Swit.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上设置文本,而不是直接设置开关的text...but --我更愿意直接在开关上设置文本。
我还有几个问题:
谢谢!
光伏
发布于 2015-03-12 23:12:41
我放弃了尝试使用开关或SwitchCompat控件的内置文本字段。最后,我提供了一个带有单独TextView字段的布局:
布局/工具栏_Swit.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
复制相似问题