ActionBar:如何拉伸动作视图布局?
ActionBar: how to stretch action view layout?
目标是在延伸到屏幕末尾的操作栏中显示编辑文本:
但结果是动作视图布局看起来被包裹了:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:background="@android:color/darker_gray"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
</LinearLayout>
bar_search.xml
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" />
menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
android:title="search"
app:actionLayout="@layout/bar_search"
app:showAsAction="ifRoom|collapseActionView" />
</menu>
谢谢!
ActionBar
在涉及 ActionView 时可能表现得有点奇怪。对于比图标稍微复杂的视图,最好将任何 UI 元素包装在 ViewGroup
中。
大多数时候 LinearLayout
或 RelativeLayout
都可以。尝试将 EditText
包裹在 RelativeLayout
.
中
目标是在延伸到屏幕末尾的操作栏中显示编辑文本:
但结果是动作视图布局看起来被包裹了:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:background="@android:color/darker_gray"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
</LinearLayout>
bar_search.xml
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" />
menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
android:title="search"
app:actionLayout="@layout/bar_search"
app:showAsAction="ifRoom|collapseActionView" />
</menu>
谢谢!
ActionBar
在涉及 ActionView 时可能表现得有点奇怪。对于比图标稍微复杂的视图,最好将任何 UI 元素包装在 ViewGroup
中。
大多数时候 LinearLayout
或 RelativeLayout
都可以。尝试将 EditText
包裹在 RelativeLayout
.