【发布时间】:2012-09-19 19:56:19
【问题描述】:
我尝试在onOptionsItemSelected(MenuItem) 方法中将操作栏的图标更改为进度条。
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.progressitem2:
mProgress = item;
mProgressCreate = mProgress;
mProgress.setActionView(R.layout.progress);
mLayout.removeView(mTable);
// Execute code that change mTable again.
return true;
}
progress.xml 文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:addStatesFromChildren="true"
android:focusable="true"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:gravity="center" >
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
style="@android:style/Widget.ProgressBar.Small"/>
</LinearLayout>
操作栏是这样创建的:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actionbar, menu);
mProgress = menu.getItem(0);
mProgressCreate = mProgress;
return true;
}
action_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/progressitem2"
android:icon="@drawable/ic_action_refresh"
android:title="Reload"
android:showAsAction="ifRoom|withText"
android:visible="true" />
</menu>
这很好用。单击操作栏图标后会显示进度图标。
我尝试将原始操作栏符号的引用保留在mProgressCreate 中,并尝试在onCreate() 方法的末尾添加操作视图:
mProgress.setActionView(mProgressCreate.getActionView());
但这不起作用...
这里有什么问题?
问候, 桑德罗
【问题讨论】:
标签: java android android-layout android-actionbar