如何在ActionBar中显示自定义视图?

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"为它设定是很重要的.应该这样做.

  • @Tomik你能看一下我的情景吗?http://stackoverflow.com/questions/16516306/sherlockactionbar-missing-title-when-customview-is-set设置自定义视图后我缺少标题. (3认同)

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的解决方案).

  • +1,实际上,它只在我添加LayoutParams后才能使用,thnx (2认同)
  • @androider是您的自定义视图.从xml中膨胀或在代码中创建. (2认同)

Bha*_*esh 8

这就是它对我有用的方法(从上面的答案中它也显示了默认标题和我的自定义视图).

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)


Mik*_*ass 6

当您希望自定义视图占据整个操作栏,甚至隐藏原生标题时,Tomik和Peterdk的答案会起作用.

但是,如果您希望自定义视图与标题并排(并在显示标题后填充所有剩余空间),那么我可以向您推荐用户Android-Developer的优秀答案:

/sf/answers/1156217681/

他的底部代码对我来说非常合适.