gpt4 book ai didi

android - 如何在操作栏中添加进度条图标?

转载 作者:行者123 更新时间:2023-11-29 21:06:36 24 4
gpt4 key购买 nike

如何将进度条图标附加到显示某些 Internet 数据加载过程的操作栏?例如:

enter image description here

加载数据时还有图标

enter image description here

最佳答案

首先,您必须创建一个自定义菜单以在您的 Activity 中使用。其次,您必须在“onCreate”中请求进度条功能。

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
// rest of your code
[....]
}//end of onCreate

然后是你的 my_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
android:id="@+id/action_refresh"
android:icon="@ drawable/ic_menu_refresh "
android:orderInCategory="100"
android:showAsAction="always"
android:title="Refresh"
android:visible="true"/>
</menu>

回到你的 Activity ,你必须膨胀这个菜单:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_refresh:
// refresh icon clicked

//show the indeterminate progress bar
setSupportProgressBarIndeterminateVisibility(true);
// Rest of your code to do re refresh here
[...]
default:
return super.onOptionsItemSelected(item);
}
}

PS:以上所有代码都使用 ActionBar 兼容库。

编辑:

好的,要隐藏操作栏图标,请执行以下操作:- 在您的函数之外创建一个菜单项:

MenuItem refreshIcon;

编辑您的 onCreateOptionsMenu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
//Initializing menu item
refreshIcon = menu.findItem(R.id.action_refresh);
return super.onCreateOptionsMenu(menu);
}

然后当您按下按钮时更改可见性:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_refresh:
// refresh icon clicked

//show the indeterminate progress bar
setSupportProgressBarIndeterminateVisibility(true);
//hiding action icon
refreshIcon.setVisible(false);
// Rest of your code to do re refresh here
[...]
default:
return super.onOptionsItemSelected(item);
}
}

最后,当您的数据更新完成后,将可见性设置为“true”并隐藏进度条:

[...] //progress inside your AsyncTask or wherever you have your update
//show Refresh Icon again
refreshIcon.setVisible(true);
//hide the indeterminate progress bar
setSupportProgressBarIndeterminateVisibility(true);

关于android - 如何在操作栏中添加进度条图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24340228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com