用户自定义动作条操作查看:越来越宽权 [英] User defined ActionBar Action View: getting the width right

查看:151
本文介绍了用户自定义动作条操作查看:越来越宽权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在冰淇淋三明治:

我期待添加一个<一个href="http://developer.android.com/reference/android/widget/AutoCompleteTextView.html">AutoCompleteTextView通过标准的动作视图机构的动作条(因的SearchView 是不可用pre-ICS和我还使用的 ActionBarSherlock ):

I'm looking to add an AutoCompleteTextView to an ActionBar through the standard Action View mechanism (because SearchView isn't available pre-ICS and I'm also using ActionBarSherlock):

<item android:id="@+id/menu_search" android:actionViewClass="com.example.AutoCompleteActionView" android:showAsAction="ifRoom" android:title="@string/address"></item>
<item android:id="@+id/menu_close" android:icon="@drawable/ic_menu_close_clear_cancel" android:showAsAction="always"></item>
<item android:id="@+id/menu_ok" android:icon="@drawable/ic_menu_ok" android:showAsAction="always"></item>

这工作,但默认情况下它不会消耗在动作条,这是我想的可用空间。

This works, however by default it does not consume the available space in the ActionBar, which I would like.

我看的<一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.0.3_r1/android/widget/SearchView.java#SearchView">source对于搜索查看并看到它是如何覆盖<一个href="http://developer.android.com/reference/android/view/View.html#onMeasure%28int,%20int%29">onMeasure,并做了同样的事情,我从AutoCompleteTextView派生我自己的类。当我这样做时,AutoCompleteTextView消耗的所有的空间的,不留空间,两个菜单项我想展示给它的权利。

I've looked at the source for the SearchView and seen how it overrides onMeasure, and done the same thing for my own class that I derived from AutoCompleteTextView. When I do this, the AutoCompleteTextView consumes all the space, leaving no space for two menu items I want to display to the right of it.

看起来好像宽度从<返回的href="http://developer.android.com/reference/android/view/View.MeasureSpec.html#getSize%28int%29">MeasureSpec.getSize()不考虑其他两个菜单项时<一href="http://developer.android.com/reference/android/view/View.MeasureSpec.html#getMode%28int%29">MeasureSpec.getMode()是<一个href="http://developer.android.com/reference/android/view/View.MeasureSpec.html#AT_MOST">MeasureSpec.AT_MOST.

It looks as though the width returned from MeasureSpec.getSize() does not take into account the other two menu items when the MeasureSpec.getMode() is MeasureSpec.AT_MOST.

任何人做过类似的话?有什么建议?

Anyone done anything similar? Any suggestions?

谢谢,      达米安

Thanks, Damian

推荐答案

我觉得这可以帮助:

与自定义布局创建一个可折叠的菜单项:

Create a collapsible menu item with a custom layout:

<?xml version="1.0" encoding="utf-8"?>
 <menu xmlns:android="http://schemas.android.com/apk/res/android" >
  <item
    android:id="@+id/menuSearch"
    android:showAsAction="always|collapseActionView" android:icon="@drawable/action_search" android:actionLayout="@layout/collapsible_absearch">
  </item>
</menu>

这是自定义布局:

<?xml version="1.0" encoding="utf-8"?>
 <AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ab_Search"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/search" />

然后在您的活动:

Then in your activity:

public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.activitymenu, menu);

    MenuItem menuItem=menu.findItem(R.id.menuSearch);

    menuItem.setOnActionExpandListener(new OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            mAbSearch=(AutoCompleteTextView) item.getActionView().findViewById(R.id.ab_Search);

            // Set your adapter and do whatever you want

            return true;
        }
    });

这篇关于用户自定义动作条操作查看:越来越宽权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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