Sab*_*bre 92 android actionbarsherlock android-actionbar
我想在操作栏中显示自定义搜索(我正在使用ActionBarSherlock).
我明白了:
但我想让自定义布局(edittext字段)占用整个可用宽度.
我按照这里的建议实现了自定义布局.
有我的自定义布局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>
Run Code Online (Sandbox Code Playgroud)
并在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);
Run Code Online (Sandbox Code Playgroud)
如何使自定义布局占用所有可用宽度actionBar
?
请帮忙.
Tom*_*mik 95
有一个技巧.您所要做的就是使用RelativeLayout
而不是LinearLayout
作为主要容器.android:layout_gravity="fill_horizontal"
为它设定是很重要的.应该这样做.
Pet*_*rdk 36
我自己也在努力,并尝试了Tomik的回答.但是,只有在向视图添加内容时,才会在开始时将布局设置为完全可用宽度.
您需要设置LayoutParams.FILL_PARENT
添加视图的时间:
//I'm using actionbarsherlock, but it's the same.
LayoutParams layout = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
getSupportActionBar().setCustomView(overlay, layout);
Run Code Online (Sandbox Code Playgroud)
这样它就完全填满了可用空间.(您可能也需要使用Tomik的解决方案).
这就是它对我有用的方法(从上面的答案中它也显示了默认标题和我的自定义视图).
ActionBar.LayoutParams layout = new ActionBar.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
// actionBar.setCustomView(view); //last view item must set to android:layout_alignParentRight="true" if few views are there
actionBar.setCustomView(view, layout); // layout param width=fill/match parent
actionBar.setDisplayShowCustomEnabled(true);//must other wise its not showing custom view.
Run Code Online (Sandbox Code Playgroud)
我注意到的是两者setCustomView(view)
和setCustomView(view,params)
视图width = match/fill parent.setDisplayShowCustomEnabled(boolean showCustom)
当您希望自定义视图占据整个操作栏,甚至隐藏原生标题时,Tomik和Peterdk的答案会起作用.
但是,如果您希望自定义视图与标题并排(并在显示标题后填充所有剩余空间),那么我可以向您推荐用户Android-Developer的优秀答案:
他的底部代码对我来说非常合适.
归档时间: |
|
查看次数: |
102591 次 |
最近记录: |