做一个Android ActionBar的而ActionView的宽度相匹配的动作条的宽度 [英] Make an Android ActionBar's ActionView's Width match the ActionBar's width

查看:96
本文介绍了做一个Android ActionBar的而ActionView的宽度相匹配的动作条的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示一个 AutoCompleteTextView ,就是始终扩大我动作条。这是我的菜单的xml:

I want to show an AutoCompleteTextView that is always expanded in my ActionBar. This is my menu 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>

这是我的布局/ 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 =总是,因为我想要的 AutoCompleteTextView 来始终可见(它会在唯一的动作条)。

I'm using showAsAction="always" because I want the AutoCompleteTextView to always be visible (it will be the only thing in the ActionBar).

我的 AutoCompleteTextView 总是右对齐的动作条键,只有100px的宽度。这是为什么?

My AutoCompleteTextView is always right-aligned in the ActionBar and only has 100px width. Why is this?

更新

由于阿图尔Ø狂热的回答,我发现我的情况下,preferred解决方案,这不不需要任何特殊的逻辑可言。我要离开我的回答正确的,因为它适用于一般情况下,但我的特定情况下,搜索查看是更好,更容易。

Thanks to Atul O Holic's answer, I found a preferred solution for my case, which doesn't require any special logic at all. I'm leaving my answer as correct because it works for the general case, but for my specific case, a SearchView is better and easier.

推荐答案

我指出了正确的方向href="http://stackoverflow.com/a/22824436/9636"> theelfismike的回答,但我可以简化它一下。他联系到被称为显示自定义视图的答案<一href="http://developer.android.com/reference/android/app/ActionBar.html#setCustomView%28android.view.View%29"><$c$c>ActionBar#setCustomView,而我试图做一个动作视图组与<一href="http://developer.android.com/reference/android/view/MenuItem.html#setActionView%28android.view.View%29"><$c$c>MenuItem#setActionView占用全宽。这可能是我的简化不会为<$​​ C $ C>动作条自定义视图工作。

这是为我工作的xml:

This is the xml that worked for me:

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

不幸的是,这个隐藏动作条的称号。 在theelfismike挂的提到了这个问题,这个问题的答案,但我不能得到的建议的解决办法的工作。

Unfortunately, this hides the ActionBar's title. An answer in the question theelfismike linked to referred to this problem, but I couldn't get the suggested workaround to work.

此外, RelativeLayout的似乎是一个动作视图所需要的根。我试图用一个的LinearLayout 与许多不同的组合 layout_gravity layout_width ,但我不能让一个的LinearLayout 填全宽。

Also, RelativeLayout seems to be the required root of an action view. I tried using a LinearLayout with many different combinations of layout_gravity and layout_width, but I couldn't get a LinearLayout to fill the full width.

这篇关于做一个Android ActionBar的而ActionView的宽度相匹配的动作条的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆