android:drawableLeft margin and/or padding

后端 未结 18 2113
猫巷女王i
猫巷女王i 2020-11-27 09:29

Is it possible to set the margin or padding for the image which we added with the android:drawableLeft?

相关标签:
18条回答
  • 2020-11-27 10:01

    Yes. use drawablePadding as follows,

    <TextView
            android:id="@+id/tvHeader"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Settings and Contents"
            android:drawableLeft="@drawable/icon_success"
            android:drawablePadding="10dp" />
    
    0 讨论(0)
  • 2020-11-27 10:01

    Make your drawable resources.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true">
            <inset android:drawable="@drawable/small_m" android:insetLeft="10dp" android:insetTop="10dp" />
        </item>
        <item>
            <inset android:drawable="@drawable/small_p" android:insetLeft="10dp" android:insetTop="10dp" />
        </item>
    </selector>
    
    0 讨论(0)
  • 2020-11-27 10:03

    android:drawablePadding will only create a padding gap between the text and the drawable if the button is small enough to squish the 2 together. If your button is wider than the combined width (for drawableLeft/drawableRight) or height (for drawableTop/drawableBottom) then drawablePadding doesn't do anything.

    I'm struggling with this right now as well. My buttons are quite wide, and the icon is hanging on the left edge of the button and the text is centered in the middle. My only way to get around this for now has been to bake in a margin on the drawable by adding blank pixels to the left edge of the canvas with photoshop. Not ideal, and not really recommended either. But thats my stop-gap solution for now, short of rebuilding TextView/Button.

    0 讨论(0)
  • 2020-11-27 10:09

    You should consider using layer-list

    Create a drawable file like this, name it as ic_calendar.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@android:color/transparent"/>
        </shape>
    </item>
    <item android:right="10dp">
        <bitmap android:gravity="center_vertical|left"
            android:src="@drawable/ic_calendar_16dp"
            android:tint="@color/red"
            />
    </item>
    </layer-list>
    

    Under layout file,

    <TextView
             android:id="@+id/tvDate"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:drawableLeft="@drawable/ic_calendar"
             android:textColor="@color/colorGrey"
             android:textSize="14sp" 
        />
    
    0 讨论(0)
  • 2020-11-27 10:12

    just remake from:

    <?xml version="1.0" encoding="utf-8"?>
    <shape
            xmlns:android="http://schemas.android.com/apk/res/android">
    
        <corners android:radius="40dp"/>
    
        <solid android:color="@android:color/white"/>
    
    </shape>
    

    to

    <layer-list
            xmlns:android="http://schemas.android.com/apk/res/android">
        <item
                android:right="@dimen/_2dp"
                android:left="@dimen/_2dp"
                android:bottom="@dimen/_2dp"
                android:top="@dimen/_2dp"
                >
            <shape
                    xmlns:android="http://schemas.android.com/apk/res/android">
    
                <corners android:radius="40dp"/>
    
                <solid android:color="@android:color/white"/>
    
            </shape>
        </item>
    
    </layer-list>
    
    0 讨论(0)
  • 2020-11-27 10:14

    As cephus mentioned android:drawablePadding will only force padding between the text and the drawable if the button is small enough.

    When laying out larger buttons you can use android:drawablePadding in conjunction with android:paddingLeft and android:paddingRight to force the text and drawable inward towards the center of the button. By adjusting the left and right padding separately you can make very detailed adjustments to the layout.

    Here's an example button that uses padding to push the text and icon closer together than they would be by default:

    <Button android:text="@string/button_label" 
        android:id="@+id/buttonId"
        android:layout_width="160dip"
        android:layout_height="60dip"
        android:layout_gravity="center"
        android:textSize="13dip"
        android:drawableLeft="@drawable/button_icon"
        android:drawablePadding="2dip"
        android:paddingLeft="30dip"
        android:paddingRight="26dip"
        android:singleLine="true"
        android:gravity="center" />  
    
    0 讨论(0)
提交回复
热议问题