【问题标题】:Remove left padding on custom action bar删除自定义操作栏上的左侧填充
【发布时间】:2014-11-14 14:47:02
【问题描述】:

首先我知道这是一个重复的问题,但没有一个答案能解决我的问题。

这是我当前的代码:

    final ActionBar actionBar = getSupportActionBar();
    actionBar.setCustomView(R.layout.action_bar);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayUseLogoEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);

XML:

    <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="fill_horizontal"
android:layout_gravity="fill_horizontal"
android:padding="0dp"
android:layout_margin="0dp"
android:background="@color/article_background">
<ImageButton android:src="@drawable/filter"
    android:layout_width="36dp"
    android:layout_height="36dp"
    android:background="#00000000"
    android:scaleType="centerInside"
    android:id="@+id/filters"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"/>

<ImageView
    android:src="@drawable/tc_logo"
    android:layout_width="36dp"
    android:layout_height="36dp"
    android:scaleType="centerInside"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/logo" />

<ImageButton android:src="@drawable/menu_button"
    android:background="#00000000"
    android:layout_width="36dp"
    android:layout_height="36dp"
    android:scaleType="centerInside"
    android:id="@+id/options"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"/>
  </LinearLayout>

还有一个左边的填充显示。

我尝试了 Stack Overflow 的以下建议:

  • 我尝试在零填充的主题中设置自定义操作栏样式。

  • 我尝试使用相对和线性布局作为自定义布局。

  • 我尝试在主题中设置自定义操作栏图标并启用 onHomeUp 以显示 1 px 图像..

  • 我尝试了布局重力和重力到 fill_horizo​​ntal 但没有奏效。

无论我做什么,它仍然会出现。

【问题讨论】:

  • 我终于以 14 dp 的负边距去了......但仍然没有答案。
  • 我仍然面临这个问题。您还有其他解决方案吗?
  • 不幸的是,没有......堆栈中充满了各种答案,但由于某种原因,它们都不适合我,它们的组合也没有。我确实发现当在 styles.xml 中设置时,这个 hack od -14dp 边距适用于所有设备、屏幕、密度......但我仍然更喜欢正常的解决方案。

标签: android android-actionbar


【解决方案1】:

我遇到了同样的问题,但没有任何帮助,直到找到 this SO question

TL;DR

  1. 通过调用setDisplayHomeAsUpEnabled(true) 让操作栏显示其向上箭头。
  2. 在“values-v14”中根据您的应用主题创建样式(这些将 适用于 SDK 版本 14 及更高版本)。
  3. 拥有此项目的新样式:&lt;item name="android:homeAsUpIndicator"&gt;@null&lt;/item&gt;。这将导致 up 消失,但图标的填充将消失。

【讨论】:

  • 很有趣,我会试试这个,如果它有效,请告诉你。
  • 不,没用。事件虽然我在我的操作栏样式中放置了@null 指示符,但显示了箭头。
  • 别了!您可以将代码摘要(代码示例、复制或其他内容)发布到这里,或者更好的是,到 GitHub 上吗?我去克隆看看怎么回事……
猜你喜欢
  • 2013-04-08
  • 1970-01-01
  • 1970-01-01
  • 2016-05-30
  • 1970-01-01
  • 2014-05-29
  • 2016-03-05
  • 2015-08-22
  • 1970-01-01
相关资源
最近更新 更多