97

我想在操作栏中显示自定义搜索(我为此使用 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>

并在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

请帮忙。

4

6 回答 6

99

这有一个窍门。您所要做的就是使用RelativeLayout而不是LinearLayout作为主容器。为它做好准备很重要android:layout_gravity="fill_horizontal"。那应该这样做。

于 2012-10-14T16:41:05.197 回答
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); 

这样它就完全填满了可用空间。(您可能也需要使用 Tomik 的解决方案)。

于 2013-05-20T15:05:32.897 回答
10

这就是它对我的工作方式(从上面的答案中,它同时显示了默认标题和我的自定义视图)。

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.

我注意到两者setCustomView(view)setCustomView(view,params)视图宽度=匹配/填充父级。setDisplayShowCustomEnabled(布尔显示自定义)

于 2015-05-08T09:14:14.563 回答
6

当您希望自定义视图占据整个操作栏,甚至隐藏本机标题时,Tomik 和 Peterdk 的答案就起作用了。

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

https://stackoverflow.com/a/16517395/614880

他在底部的代码对我来说非常有效。

于 2013-08-06T01:25:36.300 回答
3

例如,您可以定义一个包含 EditText 元素的布局文件。

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/searchfield"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:inputType="textFilter" >

</EditText> 

你可以做

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActionBar actionBar = getActionBar();
    // add the custom view to the action bar
    actionBar.setCustomView(R.layout.actionbar_view);
    EditText search = (EditText) actionBar.getCustomView().findViewById(R.id.searchfield);
    search.setOnEditorActionListener(new OnEditorActionListener() {

      @Override
      public boolean onEditorAction(TextView v, int actionId,
          KeyEvent event) {
        Toast.makeText(MainActivity.this, "Search triggered",
            Toast.LENGTH_LONG).show();
        return false;
      }
    });
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
        | ActionBar.DISPLAY_SHOW_HOME);


    }
于 2015-02-03T07:28:13.360 回答
2

在 Android 的启动器应用程序中有一个示例(我已经用它制作了一个库,在这里),在处理壁纸挑选的类 ("WallpaperPickerActivity") 中。

该示例显示您需要设置自定义主题才能使其正常工作。可悲的是,这仅适用于我使用普通框架,而不是支持库之一。

以下是主题:

样式.xml

 <style name="Theme.WallpaperPicker" parent="Theme.WallpaperCropper">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowShowWallpaper">true</item>
  </style>

  <style name="Theme.WallpaperCropper" parent="@android:style/Theme.DeviceDefault">
    <item name="android:actionBarStyle">@style/WallpaperCropperActionBar</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowActionBarOverlay">true</item>
  </style>

  <style name="WallpaperCropperActionBar" parent="@android:style/Widget.DeviceDefault.ActionBar">
    <item name="android:displayOptions">showCustom</item>
    <item name="android:background">#88000000</item>
  </style>

价值-v19/styles.xml

 <style name="Theme.WallpaperCropper" parent="@android:style/Theme.DeviceDefault">
    <item name="android:actionBarStyle">@style/WallpaperCropperActionBar</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowActionBarOverlay">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
  </style>

  <style name="Theme" parent="@android:style/Theme.DeviceDefault.Wallpaper.NoTitleBar">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
  </style>

编辑:有一种更好的方法,它也适用于支持库。只需添加这行代码,而不是我上面写的:

getSupportActionBar().setDisplayShowCustomEnabled(true);
于 2015-06-07T21:33:41.397 回答