Why my image on my custom Actionbar has unwanted left padding

匿名 (未验证) 提交于 2019-12-03 00:57:01

问题:

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:

anybody know?

回答1:

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.



回答2:

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" /> 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!