我按照此tutorial将ProgressBar
放入操作栏。但它没有显示进度条,而是显示菜单项的标题“工作”。我删除了 正在运行 的文字,然后没有显示任何内容。我只想要显示进度条。
任何人都可以帮我理解我做错了吗?
在被按下之前:
在“刷新”阶段,应显示进度条。
这是我的布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@android:style/Widget.ActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ProgressBar
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center"
android:focusable="false"/>
</FrameLayout>
这是我的菜单:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/master_refresh_action"
android:icon="@drawable/ic_refresh"
android:title="Refresh"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/pb_master_refresh"
android:actionLayout="@layout/refresh_menuitem"
android:title="working"
app:showAsAction="always"/>
</menu>
我的活动代码:
//USED FOR MENU ITEM REFRESH
private static final int STATE_IDLE = 0;
private static final int STATE_WORKING = 1;
private int mState;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_master);
mToolbar = (Toolbar) findViewById(R.id.masterToolBar);
setSupportActionBar(mToolbar);
ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
ab.setTitle("Loading");
mState = STATE_IDLE;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
invalidateOptionsMenu();
getMenuInflater().inflate(R.menu.menu_master, menu);
// itemBtnRefresh = menu.findItem(R.id.master_refresh_action);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.master_refresh_action:
refreshData();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem refresh = menu.findItem(R.id.master_refresh_action);
MenuItem progressBar = menu.findItem(R.id.pb_master_refresh);
switch (mState) {
case STATE_IDLE:
refresh.setVisible(true);
progressBar.setVisible(false);
break;
case STATE_WORKING:
refresh.setVisible(false);
progressBar.setVisible(true);
break;
default: // Should never happen!
refresh.setVisible(true);
progressBar.setVisible(false);
break;
}
return super.onPrepareOptionsMenu(menu);
}
private void setState(int state) {
mState = state;
invalidateOptionsMenu();
}
private SomeAsyncTask <>{
OnPreExecute{
setState(STATE_WORKING);
}
OnPostExecute{
setState(STATE_IDLE);
}
}
答案 0 :(得分:-1)
尝试使用always in action它会强制操作栏显示它。
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/master_refresh_action"
android:icon="@drawable/ic_refresh"
android:title="Refresh"
app:showAsAction="never"/>
<item
android:id="@+id/pb_master_refresh"
android:actionLayout="@layout/refresh_menuitem"
android:title=""
app:showAsAction="always"/>
</menu>