我想在肖像上的CollapseActionView中显示一个图标和2个项目,在ActionBar中在横向模式下显示所有3个图标。我创建了两个单独的文件menu.xml并将其放在port和land文件夹中。我的port / menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
yourapp:showAsAction="always"
android:inputType="textNoSuggestions"
yourapp:actionViewClass="android.support.v7.widget.SearchView" />
<item
android:id="@+id/maps"
android:icon="@drawable/checkin"
android:title="@string/maps"
yourapp:showAsAction="collapseActionView" />
<item
android:id="@+id/sort_by"
android:icon="@drawable/ic_action_content_sort"
android:title="@string/sort_by"
yourapp:showAsAction="collapseActionView" />
</menu>
陆地/ menu.xml文件:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
yourapp:showAsAction="always"
android:inputType="textNoSuggestions"
yourapp:actionViewClass="android.support.v7.widget.SearchView" />
<item
android:id="@+id/maps"
android:icon="@drawable/checkin"
android:title="@string/maps"
yourapp:showAsAction="always" />
<item
android:id="@+id/sort_by"
android:icon="@drawable/ic_action_content_sort"
android:title="@string/sort_by"
yourapp:showAsAction="always" />
</menu>
出于某种原因,尽管设备的方向,ActionBar总是根据port / menu.xml显示。怎么了?
答案 0 :(得分:1)
尝试使菜单无效并使用land / port menu.xml重新创建:
boolean isLandscape = false;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
if(isLandscape) {
inflater.inflate(R.land.menu, menu);
else
inflater.inflate(R.port.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE)
isLandscape = true;
else if (config.orientation == Configuration.ORIENTATION_PORTRAIT)
isLandscape = false;
invalidateOptionsMenu()
}
答案 1 :(得分:0)
经过额外的研究,我自己想通了。我只需要一个menu.xml文件而不是定义showAsAction。我的menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
yourapp:showAsAction="ifRoom"
android:inputType="textNoSuggestions"
yourapp:actionViewClass="android.support.v7.widget.SearchView" />
<item
android:id="@+id/maps"
android:icon="@drawable/checkin"
android:title="@string/maps" />
<item
android:id="@+id/sort_by"
android:icon="@drawable/ic_action_content_sort"
android:title="@string/sort_by"/>
</menu>