LoginSignup
1
0

More than 1 year has passed since last update.

【Android】selector内の画像サイズを指定

Last updated at Posted at 2021-04-12

少し詰まったので自分用メモ

selectorのサイズ指定

drawable内のxmlファイルでselector内の画像サイズを指定するには、<item>内に<layer-list>, <item>を作成してwidth, height, drawableを指定。
<selector>直下の<item>にwidth, heightを指定しても私の環境では反映されなかったので注意
(drawableは反映された)。

smple.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
	<!--ここでwidth, heightを指定しても反映されない-->
	<item android:state_checked="false">
		<layer-list>
			<!--ここでwidth, heightを指定-->
			<item
				android:width="20dp"
				android:height="10dp"
				android:drawable="@drawable/icon_off"/>
		</layer-list>
	</item>
	<item android:state_checked="true">
		<layer-list>
			<item
				android:width="20dp"
				android:height="10dp"
				android:drawable="@drawable/icon_on"/>
		</layer-list>
	</item>
</selector>

以上になるが、selector以外の場合も補足(drawable,shape)。

drawableのサイズ指定

sample.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
	<!--ここでwidth, heightを指定-->
	<item
		android:width="20dp"
		android:height="10dp"
		android:drawable="@drawable/icon"/>
</layer-list>

shapeのサイズ指定

sample.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
	   android:shape="oval">
	<solid android:color="@color/color"/>
	<!--ここでwidth, heightを指定-->
	<size
		android:width="20dp"
		android:height="10dp"/>
</shape>
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0