【问题标题】:Removing left arrow from the actionbar in android?从android中的操作栏中删除左箭头?
【发布时间】:2018-07-21 12:16:15
【问题描述】:

我想从操作栏中删除左箭头,只需要图标和标题。

我的代码:

getSupportActionBar().setIcon(R.drawable.logo);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle(R.string.dashboardtitle);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);

但这不会删除左箭头。任何帮助!

【问题讨论】:

  • 我本来希望setDisplayHomeAsUpEnabled(false) 这样做。您是否正在做与自定义主题中的操作栏相关的任何事情?
  • 不只是我想从操作栏中删除左箭头
  • 你运行的Android SDK是什么版本的?
  • 安卓4.2.2版本
  • 你是用ActionBar sherlock还是原生的android ActionBar?

标签: android android-actionbar


【解决方案1】:

根据规范android specification: 要启用向上导航的图标(在图标旁边显示“向上”指示符),请在 ActionBar 上调用 setDisplayHomeAsUpEnabled(true):

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    ...
}

所以你应该设置 actionBar.setDisplayHomeAsUpEnabled(false);

更新: 我用 ActionBar sherlock 测试下一个代码,它的工作。没有后退箭头:

getSupportActionBar().setIcon(R.drawable.ic_launcher);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setTitle(R.string.about_event_location);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);

【讨论】:

  • 还有一点黑色空间
【解决方案2】:

您需要将此添加到您的 actionBar 主题中。您可以将箭头图像替换为您自己的透明图像

<item name="android:homeAsUpIndicator">@drawable/action_arrow</item>

【讨论】:

  • 这成功了!我必须将新的可绘制对象设置为某个宽度的透明图像,因为使用 @android:color/transparent 不包括应用图标左侧的任何填充。
  • 可以使用@null 来避免创建透明图像资源。 @null
【解决方案3】:
getActionBar().setHomeAsUpIndicator(R.drawable.action_arrow);

其中 action_arrow 是一个 25x10 像素的透明 png。 夏洛克的相同代码是:

getSupportActionBar().setHomeAsUpIndicator(R.drawable.action_arrow);

【讨论】:

    【解决方案4】:

    @vivek:效果很好。我不建议像 Tim Cooper 所说的那样使用 @android:color/transparent ,因为 Title 是靠左对齐的。所以空图像更好

    【讨论】:

    • 这看起来更像是评论而不是答案。
    【解决方案5】:

    在 AndroidManifest.xml 中

    删除以下代码,这将从您的 ActionBar 中删除后退按钮

    android:parentActivityName=".Main.MainActivity">
    <meta-data
    android:name="android.support.PARENT_ACTIVITY"
    android:value=".Main.MainActivity" />
    

    保持如下:

    <activity
    android:name=".YourActivity"
    android:label="@string/your_activity_label">
    </activity>
    

    【讨论】:

    • 我遇到了丑陋的后退箭头问题,正是因为我必须添加 android:parentActivityName 属性才能构建正确的后退堆栈!您的解决方案还可能破坏系统后退按钮的预期行为。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    相关资源
    最近更新 更多