【问题标题】:Make an Android ActionBar's ActionView's Width match the ActionBar's width使 Android ActionBar 的 ActionView 的宽度与 ActionBar 的宽度匹配
【发布时间】:2014-04-02 21:57:07
【问题描述】:

我想显示一个始终在我的ActionBar 中展开的AutoCompleteTextView。这是我的菜单 xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/visibility_menu_search"
        android:icon="@drawable/ic_action_search"
        android:showAsAction="always"
        android:actionLayout="@layout/visibility_search" />
</menu>

这是我的layout/visibility_search.xml

<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/visibility_search_auto_complete_text_view" />

这是我的活动:

public class VisibilityActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(layout.visibility_activity);
        getActionBar().setDisplayShowTitleEnabled(false);
    }

    @Override
     public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.visibility, menu);
        return super.onCreateOptionsMenu(menu);
    }
}

我使用showAsAction="always" 是因为我希望AutoCompleteTextView 始终可见(它将是ActionBar 中唯一的东西)。

我的AutoCompleteTextViewActionBar 中始终是右对齐的,并且只有 100 像素的宽度。这是为什么呢?

更新

感谢Atul O Holic's answer,我找到了适合我的案例的首选解决方案,它根本不需要任何特殊逻辑。我将 my answer 保留为正确,因为它适用于一般情况,但对于我的具体情况,SearchView 更好更容易。

【问题讨论】:

  • 必须是AutoCompleteTextView吗?你见过this吗? SearchView 将在用户点击它时展开和折叠,并且它具有提供建议列表的功能。
  • 我希望它一直被扩展。 ActionBar 中将没有其他内容,因此让用户点击图标以展开 SearchView 只是不必要的工作。

标签: android android-actionbar searchview


【解决方案1】:

theelfismike's answer 为我指明了正确的方向,但我能够稍微简化一下。他链接的答案是指使用ActionBar#setCustomView 显示自定义视图,而我正在尝试使用MenuItem#setActionView 设置动作视图占据整个宽度。我的简化可能不适用于 ActionBar 自定义视图。

这是对我有用的 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="wrap_content"
                android:layout_height="match_parent">
    <EditText xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:id="@+id/visibility_search_edit_text"/>
</RelativeLayout>

不幸的是,这隐藏了ActionBar 的标题。 An answer in the question theelfismike linked to 提到了这个问题,但我无法让 suggested workaround 工作。

另外,RelativeLayout 似乎是动作视图的必需根。我尝试使用LinearLayoutlayout_gravitylayout_width 的许多不同组合,但我无法获得LinearLayout 来填充整个宽度。

【讨论】:

  • 这实际上并没有为我隐藏标题。解决了。谢谢
  • 谢谢!您对 RelativeLayout 的评论为我节省了一些时间。
  • 谢谢你...我刚刚将我的 LinearLayout 包裹在相对布局中,它完美运行...!
【解决方案2】:

查看此解决方案:How to display custom view in ActionBar?

我想我必须做类似的事情。

我认为神奇的部分是它必须是一个 RelativeLayout 内的 LinearLayout 并设置此属性:android:layout_gravity="fill_horizontal"

【讨论】:

  • 你知道如何防止这种情况隐藏ActionBar的标题吗?
【解决方案3】:

我同意@Emmanuel 在使用 SearchView 的上下文中,因为它是一个内置视图并且旨在搜索。 :) 它也特别适用于 ActionBar。

I want it to be expanded all the time.

添加以下内容将始终默认保持展开状态(除非用户自己关闭它),

searchView.setIconified(false);

我们还可以通过 ContentProvider 向 SearchView 添加自动建议。 Here's 来自 Google 的同一文档。你可能还喜欢this.

试一试。玩得开心。 :)

【讨论】:

  • 我不想让用户选择取消图标化。我将坚持使用AutoCompleteTextView,因为它现在看起来最简单,但这看起来不错。谢谢!
  • 我试过SearchView,它有SearchView#setIconfiedByDefault,当设置为false时,它会阻止图标化。这是我想要的确切行为。我会更新问题。
  • 很高兴它为您提供了正确轨道的提示。 :D 任务成功。 :)