【发布时间】:2015-09-01 17:47:11
【问题描述】:
使用自定义 ActionBar 和两个操作栏项目运行我的应用程序,在不同设备上显示不同。在一台设备上,项目显示在操作栏徽标的左侧,而在另一台设备上,操作项目显示在右侧。
操作栏视图:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:focusable="true">
<ImageView
android:id="@+id/btn_slide"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@null"
android:scaleType="centerInside"
android:src="@drawable/logo_advanced"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
操作栏项目:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:compat="http://schemas.android.com/apk/res-auto">
<!-- logout action -->
<item android:id="@+id/action_logout"
android:icon="@drawable/logo1"
android:title="logout"
android:showAsAction="never"
compat:showAsAction="never"
/>
<!-- go to position-->
<item android:id="@+id/action_search_position"
android:icon="@drawable/position_a"
android:title="search_position"
android:showAsAction="always"
compat:showAsAction="always"
/>
活动回调:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
在扩展Activity 的主要活动的OnCreate 中动态操作栏初始化:
ActionBar actionBar=getActionBar();
View mActionBarView = getLayoutInflater().inflate(R.layout.action_bar, null);
actionBar.setCustomView(mActionBarView);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
- 操作栏徽标左侧的操作项的唯一设备是 Galaxy 2(带有内置溢出菜单按钮的旧设备)。在此设备上,WhatsApp 和 YouTube 应用也以这种方式出现(与具有适当外观的新设备相反),但另一方面,Tinder 和 Gmail 等其他应用在所有设备上保持相同的外观。
知道为什么吗?
【问题讨论】:
-
我猜这是因为一台设备打开了开发人员选项“强制 RTL 布局方向”。在设备的设置应用中查看。
-
你能告诉我那个选项吗?我找不到它。
-
打开设置应用程序(设备的)-> 一直向下滚动-> 开发人员选项-> 仔细查看类似“强制 RTL 布局方向”之类的内容。
-
我的设备上没有这样的选项(Galaxy 2)
-
哦,我猜我猜错了,可能是因为 ActionBar 与旧版本 Android 的兼容性问题。