我正在尝试在ActionBarActivity中使用进度表,以便在运行api 7的设备上使用片段。
活动正常的结果,这就是我想要的结果。:
行动不良的结果:
我的代码:
//The progress bar only works with a normal activity but not with an ActionBarActivity
public class TestActivity extends Activity { //now produces image 1, replace this with a ActionBarActivity and it produces image 2.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.bluethooth, menu);
//Code below is from http://stackoverflow.com/questions/20402912/android-actionbar-compatibility-menuitem-setactionviewview
MenuItem item = menu.findItem(R.id.menu_refresh);
MenuItemCompat.setActionView(item, R.layout.actionbar_indeterminate_progress);
return true;
}
}
activity_register.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RegisterActivity"
tools:ignore="MergeRootFrame" />
actionbar_indeterminate_progress.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<ProgressBar android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center"/>
</FrameLayout>
bluethooth.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_refresh"
android:checkable="false"
android:orderInCategory="1"
android:showAsAction="ifRoom"
android:actionLayout="@layout/actionbar_indeterminate_progress"/>
<item android:id="@+id/menu_scan"
android:title="@string/menu_scan"
android:orderInCategory="100"
android:showAsAction="ifRoom|withText"/>
<item android:id="@+id/menu_stop"
android:title="@string/menu_stop"
android:orderInCategory="101"
android:showAsAction="ifRoom|withText"/>
</menu>
我也尝试过:
SupportMenuItem item = (SupportMenuItem) menu.findItem(R.id.menu_refresh);
MenuItemCompat.setActionView(item, R.layout.actionbar_indeterminate_progress);
和
SupportMenuItem item = (SupportMenuItem) menu.findItem(R.id.menu_refresh);
item.setActionView(R.layout.actionbar_indeterminate_progress);
但上述代码都没有。
我做错了什么?
编辑:如何将问题标记为已回答?