how to specify width and height of a drawable item

后端 未结 3 955
梦如初夏
梦如初夏 2021-01-11 12:23

Hi is there anyway of providing width and height of a drawable defined in drawable.xml in drawable folder.

Example:

 

        
相关标签:
3条回答
  • 2021-01-11 13:03

    You can specify inside the <item> tag the width and height of the drawable:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_pressed="true"
           android:width="100dp"
           android:height="20dp"
           android:drawable="@drawable/button_pressed" /> <!-- pressed -->
     <item android:state_focused="true"
           android:width="100dp"
           android:height="20dp"
           android:drawable="@drawable/button_focused" /> <!-- focused -->
     <item android:drawable="@drawable/button_normal" /> <!-- default -->
    

    0 讨论(0)
  • 2021-01-11 13:09

    You can use attribute width and height in API level 23 and higher:

    <item
        android:width="18dp" android:height="18dp"
        android:drawable="@drawable/icon_ratingstar"/>
    

    For lower API levels, I do not know a way of specifying width and height directly. The best practice is scaling drawables as far as I know.

    0 讨论(0)
  • 2021-01-11 13:13

    layer-list will help: here change values according to your requirement

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <size
                android:width="20dp"
                android:height="20dp" />
    
            <solid android:color="@android:color/black" />
        </shape>
    </item>
    <item
        android:width="20dp"
        android:height="20dp"
        android:drawable="@drawable/slider_knob" />
    

    0 讨论(0)
提交回复
热议问题