How to specify the width and height of the drawn element

In any case, Hi provides the width and height of the selection in drawable.xml in the drop-down folder.

Example:

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

I wanted to specify the width and height, perhaps somehow using the scale tag, inside which I tried but did not work. This is my code:

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/icon_ratingstar"> <scale android:scaleWidth="20" android:scaleHeight="20" android:drawable="@drawable/icon_ratingstar" /> </item> </selector> 
+7
source share
3 answers

You can specify inside the <item> width and height 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 --> 

-14
source

layer-list will help: here change the values ​​to suit 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" /> 

+2
source

You can use the width and height attributes in the API 23 and above:

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

For lower API levels, I don’t know how to directly specify width and height . As far as I know, the best practice is to scale drawings.

0
source

All Articles