java 用不确定的 ProgressBar 替换 ActionBar 菜单项图标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7142722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:49:58  来源:igfitidea点击:

Replacing an ActionBar menu item icon with an indeterminate ProgressBar

javaandroidandroid-3.0-honeycombandroid-actionbar

提问by Melllvar

I would like to add an indeterminate progress bar to the Honeycomb ActionBar, so that any time the user presses "Refresh", the refresh icon temporarily turns into an indeterminate progress bar, until the task completes. The Email app does this already, but I can't figure out how.

我想在 Honeycomb ActionBar 中添加一个不确定的进度条,这样每当用户按下“刷新”时,刷新图标就会暂时变成一个不确定的进度条,直到任务完成。电子邮件应用程序已经做到了这一点,但我不知道如何做到这一点。

Any advice?

有什么建议吗?

采纳答案by Femi

Hard to tell exactly how the Email app does it, but you may want to stay simple and just call setIconwith the id of a StateDrawableXML file, and then just change the state using a Timer.

很难确切地说明电子邮件应用程序是如何做到的,但您可能希望保持简单,只需使用XML 文件的 id调用setIconStateDrawable,然后使用 Timer 更改状态。

回答by larham1

To clarify Jon O's answer, the key is to set and unset an action view on the refresh action. This works in both ActionBarSherlock and native 4.x action bar. The following snippet will put the progress indeterminate view on top of the refresh icon, assuming the refresh menu item has ID 'refresh_option' and the replacement layout (which has a ProgressBar) is in layout 'progress_wheel':

为了澄清 Jon O 的答案,关键是在刷新操作上设置和取消设置操作视图。这适用于 ActionBarSherlock 和原生 4.x 操作栏。下面的代码片段将把进度不确定视图放在刷新图标的顶部,假设刷新菜单项的 ID 为“refresh_option”,并且替换布局(有一个 ProgressBar)在布局“progress_wheel”中:

 MenuItem item = abmenu.findItem(R.id.refresh_option);
 LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 View abprogress = inflater.inflate(R.layout.progress_wheel, null);
 item.setActionView(abprogress);

Unset the progress view, and the refresh icon will return to visibility:

取消设置进度视图,刷新图标将恢复可见:

 item.setActionView(null);

See a more detailed exampleon github.

在 github 上查看更详细的示例

回答by aga

To simplify larham1's answer: you don't even need to inflate new action view itself because MenuItemhas the methodwhich accepts id of action layout, so you can simply write:

为了简化larham1的回答:你甚至不需要夸大新动作视图本身,因为MenuItem方法,它接受的行动布局的ID,所以你可以简单地写:

item.setActionView(R.layout.progress_bar);

回答by Jon O

It turns out that Google has posted an example of doing exactly this as a part of their broader ActionBarCompat compatibility project. Have a look.

事实证明,作为其更广泛的 ActionBarCompat 兼容性项目的一部分,Google 已经发布了一个完全执行此操作的示例。 看看

回答by alexx

I'm using the code provided at the original issue here: https://github.com/JakeWharton/ActionBarSherlock/issues/425

我正在使用原始问题中提供的代码:https: //github.com/JakeWharton/ActionBarSherlock/issues/425

Except for android:layout_widthand android:layout_height(in the actionbar_indeterminate_progress.xml) I use 32dp; as this was the way it was done in ActionBarCompat: http://developer.android.com/resources/samples/ActionBarCompat/res/layout-v11/actionbar_indeterminate_progress.html

除了android:layout_widthandroid:layout_height(在 actionbar_indeterminate_progress.xml 中)我使用32dp; 因为这是在 ActionBarCompat 中完成的方式:http: //developer.android.com/resources/samples/ActionBarCompat/res/layout-v11/actionbar_indeterminate_progress.html

回答by Heigo

You can easily do it by:

您可以通过以下方式轻松完成:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_refresh: 
                item.setActionView(new ProgressBar(this));
                break;
        }
        return super.onOptionsItemSelected(item);