如何在 ActionBar 中显示自定义视图? [英] How to display custom view in ActionBar?
问题描述
我想在操作栏中显示自定义搜索(为此我使用 ActionBarSherlock).
I want to display custom search in actionbar (I'm using ActionBarSherlock for that).
我明白了:
但我想让自定义布局(edittext 字段)占据整个可用宽度.
But I want make custom layout (edittext field) to occupy the entire available width.
我已经按照此处的建议实现了自定义布局.
I've implemented custom layout as suggested here.
有我的自定义布局 search.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="?attr/actionButtonStyle"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_horizontal"
android:focusable="true" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|fill_horizontal" >
<EditText
android:id="@+id/search_query"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|center"
android:background="@drawable/bg_search_edit_text"
android:imeOptions="actionSearch"
android:inputType="text" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:src="@drawable/ic_search_arrow" />
</FrameLayout>
</LinearLayout>
在 MyActivity
中:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setIcon(R.drawable.ic_action_search);
LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.search, null);
actionBar.setCustomView(v);
如何让自定义布局占据 actionBar
的所有可用宽度?
How can I make custom layout to occupy all the available width of actionBar
?
请帮忙.
推荐答案
对此有一个技巧.您所要做的就是使用 RelativeLayout
而不是 LinearLayout
作为主容器.为它设置 android:layout_gravity="fill_horizontal"
很重要.应该这样做.
There is a trick for this. All you have to do is to use RelativeLayout
instead of LinearLayout
as the main container. It's important to have android:layout_gravity="fill_horizontal"
set for it. That should do it.
这篇关于如何在 ActionBar 中显示自定义视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!