我正在使用'AppCompact'库并遇到布局/定位方面的一些问题.我想将"应用程序"图标放在"ActionBar"的右侧.一种方法是在工具栏中定义一个按钮,但有一个标准方法来设置ActionBar右侧的App图标和向上按钮吗?

正如您在上图中看到的那样,图标位于左侧,我希望它位于右侧.任何帮助,将不胜感激.
Ps:对于可能遇到我的问题的人,可以使用此代码轻松修复此问题.将此代码添加到清单:
<application android:supportsRtl="true">
Run Code Online (Sandbox Code Playgroud)
然后在Oncreate上编写此代码:
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
Run Code Online (Sandbox Code Playgroud)
Apu*_*rva 17
android没有办法在动作栏的右侧设置应用程序图标,但你仍然可以这样做.
比如创建一个菜单 main_menu.xml
<?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/menu_item"
android:icon="@drawable/your_app_icon"
android:title="@string/menu_item"
app:showAsAction="always"/> //set showAsAction always
//and this should be the only menu item with show as action always
</menu>
Run Code Online (Sandbox Code Playgroud)
现在只需覆盖 onCreateOptionsMenu您的活动类.
添加这个 MainActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
Run Code Online (Sandbox Code Playgroud)
完成!您的应用程序图标现在将显示在ActionBar的右侧.
如果菜单中有多个项目,则覆盖 onPrepareOptionsMenu活动类并设置setEnabled(false)具有应用程序图标的菜单项,这样做可以防止您的图标被点击.
@Override
public boolean onPrepareOptionsMenu(Menu menu){
menu.findItem(R.id.menu_item).setEnabled(false);
return super.onPrepareOptionsMenu(menu);
}
Run Code Online (Sandbox Code Playgroud)
现在你的MainActivity.java文件看起来像
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.menu_item: //this item has your app icon
return true;
case R.id.menu_item2: //other menu items if you have any
//add any action here
return true;
case ... //do for all other menu items
default: return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onPrepareOptionsMenu(Menu menu){
menu.findItem(R.id.menu_item).setEnabled(false);
return super.onPrepareOptionsMenu(menu);
}
Run Code Online (Sandbox Code Playgroud)
这是您可以用来在右侧设置应用程序图标的唯一技巧.
| 归档时间: |
|
| 查看次数: |
20984 次 |
| 最近记录: |