如何显示在动作条自定义视图? [英] 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.
我实现自定义布局的建议<一href="http://stackoverflow.com/questions/12750013/actionbar-logo-centered-and-action-items-on-sides">here.
I've implemented custom layout as suggested here.
还有就是我的自定义布局 search.xml
:
There is my custom layout 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);
我怎样才能让自定义布局中占据动作条
?
帮助,请。
推荐答案
有一个窍门这一点。你所要做的就是用 RelativeLayout的
而不是的LinearLayout
作为主容器。它有很重要安卓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.
这篇关于如何显示在动作条自定义视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!