用户定义的 ActionBar Action View:获得正确的宽度

发布于 2025-01-04 23:50:36 字数 1830 浏览 1 评论 0原文

关于冰淇淋三明治:

我希望通过标准 AutoCompleteTextView 添加到 ActionBar a href="http://developer.android.com/guide/topics/ui/actionbar.html#ActionView">Action View 机制(因为 SearchView 在 ICS 之前不可用,我也在使用 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>

这是可行的,但是默认情况下它不会消耗 ActionBar 中的可用空间,这是我想要的。

我查看了 SearchView 的源 并了解它如何覆盖 onMeasure,并为我自己的类做了同样的事情派生自 AutoCompleteTextView。当我这样做时,AutoCompleteTextView 会占用所有空间,没有为我想在其右侧显示的两个菜单项留下空间。

看起来好像是从 MeasureSpec.getSize()MeasureSpec.getMode 时,不考虑其他两个菜单项()MeasureSpec.AT_MOST

有人做过类似的事情吗?有什么建议吗?

谢谢, 达米安

On Ice Cream Sandwich:

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.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

傾旎 2025-01-11 23:50:36

我认为这可能会有所帮助:

使用自定义布局创建可折叠菜单项:

<?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" />

然后在您的活动中:

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;
        }
    });

I think this could help:

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>

This is the custom layout:

<?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;
        }
    });
來不及說愛妳 2025-01-11 23:50:36

我无法让 Matthias 的建议在代码中工作(也许我遗漏了一些东西),但是在我的 Action View 中添加一个 minWidth 属性到最上面的元素就成功了。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="64dip"
    android:minWidth="64dip"
    android:layout_height="match_parent"
    android:gravity="center" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:layout_gravity="center"
        android:id="@+id/imageViewAnimatedProgressSpinner"
        android:background="@drawable/animated_progress_spinner" />

</FrameLayout>

I wasn't able to get Matthias's suggestion to work in code (maybe I was missing something), but adding a minWidth attribute to the topmost element in my Action View did the trick.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="64dip"
    android:minWidth="64dip"
    android:layout_height="match_parent"
    android:gravity="center" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:layout_gravity="center"
        android:id="@+id/imageViewAnimatedProgressSpinner"
        android:background="@drawable/animated_progress_spinner" />

</FrameLayout>
旧城烟雨 2025-01-11 23:50:36

这对您有帮助吗?:

    ActionBar.LayoutParams layoutParams = new LayoutParams(android.app.ActionBar.LayoutParams.MATCH_PARENT, android.app.ActionBar.LayoutParams.MATCH_PARENT);
    View customNav = LayoutInflater.from(getSupportActionBar().getThemedContext()).inflate(R.layout.your_layout, null);
    getSupportActionBar().setCustomView(customNav, layoutParams);
    getSupportActionBar().setDisplayShowCustomEnabled(true);

This helps you?:

    ActionBar.LayoutParams layoutParams = new LayoutParams(android.app.ActionBar.LayoutParams.MATCH_PARENT, android.app.ActionBar.LayoutParams.MATCH_PARENT);
    View customNav = LayoutInflater.from(getSupportActionBar().getThemedContext()).inflate(R.layout.your_layout, null);
    getSupportActionBar().setCustomView(customNav, layoutParams);
    getSupportActionBar().setDisplayShowCustomEnabled(true);
数理化全能战士 2025-01-11 23:50:36

可能晚了好几年,但是如果有人来找你的操作视图,请执行以下操作,将它们设置为默认操作栏图标,

//Define a style which extends the themed action button in styles.xml, if you want you can use references to have dynamic styles.. this is an example for holo theme
<style name="ActionButtonStyle" parent="@android:style/Widget.Holo.ActionButton">

</style>

//For your action view, use this as the style, I find the issue with Progress bars and hence //the example,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    style="@style/ActionButtonStyle" >

    <ProgressBar
        android:id="@+id/progressBar2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ProgressBar>

</LinearLayout>

might be light years late, but if someone comes looking do the following on your action views to get them styled as the default action bar icons,

//Define a style which extends the themed action button in styles.xml, if you want you can use references to have dynamic styles.. this is an example for holo theme
<style name="ActionButtonStyle" parent="@android:style/Widget.Holo.ActionButton">

</style>

//For your action view, use this as the style, I find the issue with Progress bars and hence //the example,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    style="@style/ActionButtonStyle" >

    <ProgressBar
        android:id="@+id/progressBar2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ProgressBar>

</LinearLayout>
转身泪倾城 2025-01-11 23:50:36
<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>

归功于这篇文章: 使 Android ActionBar 的 ActionView 的宽度匹配ActionBar 的宽度。这将使它伸展。

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

Credit to this post: Make an Android ActionBar's ActionView's Width match the ActionBar's width. This will make it stretch.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文