【问题标题】:Why my image on my custom Actionbar has unwanted left padding为什么我的自定义操作栏上的图像有不需要的左填充
【发布时间】:2015-02-24 10:40:51
【问题描述】:

我通过扩展自定义视图自定义了ActionBar。 我的自定义操作栏 XML 布局是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="fill_horizontal"
    android:background="@drawable/actionbar_bg" >

    <TextView
        android:id="@+id/ab_main_text"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="16dp"
        android:gravity="right|center_vertical"
        android:text="heaer_text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#ffffff" />

    <ImageView
        android:id="@+id/ab_main_img0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:clickable="true"
        android:src="@drawable/ab_main_img_layer_list" />

</RelativeLayout>

但我不知道为什么我的ImageView 在运行时有一个不需要的左填充!!!如下图:

有人知道吗?

【问题讨论】:

标签: android android-actionbar


【解决方案1】:

似乎当您在自定义操作栏 xml 布局的 ImageView(不是 TextView)中使用“wrap_content”或“match_parent”时,您的操作栏中将有填充,否则您没有。

所以请尝试如下更改您的 xml:为 ImageView 设置固定大小(您还可以添加一些 margin_left 以获得更好的外观)。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="fill_horizontal"
    android:background="@drawable/actionbar_bg" >

    <TextView
        android:id="@+id/ab_main_text"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="16dp"
        android:gravity="right|center_vertical"
        android:text="heaer_text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#ffffff" />

    <ImageView
        android:id="@+id/ab_main_img0"
       android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"
        android:clickable="true"
        android:src="@drawable/ab_main_img_layer_list" />

</RelativeLayout>

编辑:刚刚编辑了我的答案,以便更准确地解释问题:)。希望它也能帮助其他人。

【讨论】:

  • 不,我的图像没有缩放,因为我在上面设置了一个选择器,所以当我触摸我的图像时,它下面会出现一个深色背景,我又看到了左边的填充!
  • 好吧,我实际上尝试了你的方式,我也有左边的填充,但是在查看下面的教程并尝试他们建议的固定尺寸和所有内容之后,它对我有用。 javatechig.com/android/…也许你也可以试试看看会发生什么。它也可能与您为操作栏充气的方式有关。深色背景可能是原生操作栏。
  • 感谢@A B,我阅读了您的链接并编写了它的脚本,我也编写了您的脚本,两者都没有填充!所以我将“你的脚本和链接脚本”添加到我的脚本中,然后我发现当我在 ImageView 中使用“wrap_content”或“match_parent”时我有填充,否则没有!
  • 很好用!能否请您将我的答案标记为正确?谢谢! :)
【解决方案2】:

你应该试试这个

android:layout_marginLeft="5dp"

在 ImageView 内

<ImageView
    android:id="@+id/ab_main_img0"
    android:layout_width="50px"
    android:layout_height="50px"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:clickable="true"
    android:layout_marginLeft="5dp"
    android:src="@drawable/ab_main_img_layer_list" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多