我正在使用支持库V7中的ActionBarActivity。 但是,当我在菜单xml中指定android:showAsAction =“always”时,它不会生效。我必须在onCreateOptionsMenu()方法的Java代码中执行此操作,以查找项目并手动设置属性。我的代码有什么问题吗?为什么会这样?
以下是我的活动代码,大部分来自document sample:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(false);
Tab tab = actionBar
.newTab()
.setText("ArtistFragment")
.setIcon(android.R.drawable.ic_input_add)
.setTabListener(
new TabListener<ArtistFragment>(this, "ArtistFragment", ArtistFragment.class));
actionBar.addTab(tab);
tab = actionBar
.newTab()
.setText("AlbumFragment")
.setIcon(android.R.drawable.btn_radio)
.setTabListener(
new TabListener<AlbumFragment>(this, "AlbumFragment", AlbumFragment.class));
actionBar.addTab(tab);
}
public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
/** Constructor used each time a new tab is created.
* @param activity The host Activity, used to instantiate the fragment
* @param tag The identifier tag for the fragment
* @param clz The fragment's Class, used to instantiate the fragment
*/
public TabListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
/* The following are each of the ActionBar.TabListener callbacks */
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
mFragment.setHasOptionsMenu(true); //MUST manually set here
ft.add(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}
public static class ArtistFragment extends Fragment {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu1, menu);
// Manually set will take effect
menu.findItem(R.id.menu_item1)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
}
public static class AlbumFragment extends Fragment {
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu2, menu);
// XML set android:showAsAction="always" does not work
}
}
}
以下是两个菜单xml文件:
menu1.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_item1"
android:icon="@android:drawable/ic_menu_view"
android:title="menu1"
android:showAsAction="always"/>
</menu>
menu2.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_item2"
android:icon="@android:drawable/ic_menu_view"
android:title="menu2"
android:showAsAction="always"/>
</menu>
答案 0 :(得分:11)
诀窍是我必须修改我的菜单xml文件:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mytest="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_item1"
android:icon="@android:drawable/ic_media_ff"
android:title="menu1"
mytest:showAsAction="always"/>
</menu>
从官方文件中可以解释为什么这是必要的:
使用支持库中的XML属性
请注意,上面的showAsAction属性使用标记中定义的自定义命名空间。当使用支持库定义的任何XML属性时,这是必要的,因为旧设备上的Android框架中不存在这些属性。因此,您必须使用自己的命名空间作为支持库定义的所有属性的前缀。
答案 1 :(得分:0)
使用此代码解决了我的问题:
我用android:showAsAction="always"
修改了app:showAsAction="always"
,问题解决了
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_add_projects"
app:showAsAction="always"
android:icon="@drawable/ic_action_add_project"
android:title="@string/add_project" />
</menu>