Why my image on my custom Actionbar has unwanted l

2019-07-12 23:36发布

I customized the ActionBar by inflating a custom view. and my custom action bar XML layout is:

<?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>

but I don't know why there is an unwanted left padding for my ImageView at run time!!! like image below:

http://8pic.ir/images/ar3ykgqke9an68kj6hoc.png

anybody know?

2条回答
Rolldiameter
2楼-- · 2019-07-12 23:56

It seems that when you use "wrap_content" or "match_parent" in your ImageView (not TextView) on your custom action bar xml layout, you will have padding in your action bar, otherwise you don't have.

So please try changing your xml as below: setting fixed size to the ImageView (and you can also add some margin_left for a better look).

<?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>

EDIT: Just edited my answer for a more accurate explanation of the issue :). Hope it will help others as well.

查看更多
一夜七次
3楼-- · 2019-07-13 00:17

You should try this

android:layout_marginLeft="5dp"

inside 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" />
查看更多
登录 后发表回答