Padding in ImageView is not Working

前端 未结 3 717
隐瞒了意图╮
隐瞒了意图╮ 2021-02-18 23:24

I want to make a screen which contains a top bar and an image. I use TextView for the top bar and ImageView for the image. I want to set padding only to the image view. My xml i

相关标签:
3条回答
  • 2021-02-18 23:46

    On the ImageView use

    android:src="@drawable/image"
    

    instead of

    android:background="@drawable/image"
    

    src tag honors the padding whereas background tag does not. In some cases, esp. on clickable icon, it is a bad idea to use layout_margin instead of padding, as padding belongs to the view and makes the click area bigger!.

    0 讨论(0)
  • 2021-02-18 23:49

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

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

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

    update

    you can set cropToPadding also:

    <ImageView 
    ...
       android:cropToPadding="true"
       android:padding="15dp"
       android:scaleType="centerInside"  
    ...
    
    0 讨论(0)
  • 2021-02-18 23:56

    The only solution for this is adding the imageview 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>
    
    0 讨论(0)
提交回复
热议问题