Filling in ImageView does not work

I want to create a screen containing the top bar and image. I use TextView for the top bar and ImageView for the image. I want to install an addition only to the representation of the image. My xml is below. The add-on action does not work. Can someone explain why and what to do?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background" > <TextView android:id="@+id/topBar" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="@string/home" android:textSize="20sp" android:textColor="#ffffff" android:textStyle="bold" android:shadowColor="#000000" android:shadowRadius="1" android:shadowDy="-1" android:gravity="center" android:background="@drawable/navBar"/> <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="10dp" android:paddingTop="10dp" android:paddingRight="10dp" android:paddingBottom="10dp" android:layout_below="@+id/topBar" android:background="@drawable/image"/> </RelativeLayout> 
+7
android android-layout padding imageview
source share
3 answers

You should use layout_margin instead of padding , so it should be as follows:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background" > <TextView android:id="@+id/topBar" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="@string/home" android:textSize="20sp" android:textColor="#ffffff" android:textStyle="bold" android:shadowColor="#000000" android:shadowRadius="1" android:shadowDy="-1" android:gravity="center" android:background="@drawable/navBar"/> <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_marginRight="10dp" android:layout_marginBottom="10dp" android:layout_below="@+id/topBar" android:background="@drawable/image"/> navBar" /> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background" > <TextView android:id="@+id/topBar" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="@string/home" android:textSize="20sp" android:textColor="#ffffff" android:textStyle="bold" android:shadowColor="#000000" android:shadowRadius="1" android:shadowDy="-1" android:gravity="center" android:background="@drawable/navBar"/> <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_marginRight="10dp" android:layout_marginBottom="10dp" android:layout_below="@+id/topBar" android:background="@drawable/image"/> 

and you can specify 1 layout_margin for all directions, so instead of using

 android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_marginRight="10dp" android:layout_marginBottom="10dp" 

you can use:

 android:layout_margin="10dip" 

I hope this is what you are looking for. give me feedback otherwise;)

Update

you can set cropToPadding also:

 <ImageView ... android:cropToPadding="true" android:padding="15dp" android:scaleType="centerInside" ... 
+10
source share

The only solution for this is to add the image to another container.

  <FrameLayout android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="center" android:layout_marginRight="10dp" android:padding="10dp" > <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:background="@drawable/ic_delete_blue" /> </FrameLayout> 
+5
source share

In ImageView use

 android:src="@drawable/image" 

instead

 android:background="@drawable/image" 
Tag

src marks the differences while the background tag does not work. In some cases, especially. on a clickable icon, it is a bad idea to use layout_margin instead of filling, since the indentation belongs to the view and makes the click area larger!

+3
source share

All Articles