ボタンの画像があります。それを利用するには、状態ごとに 1 つの追加画像が必要です: 1. 無効 2. 選択済み 3. 押されたなど..
iOS では、これらの追加の状態はすべて自動的に処理され、提供された元の画像から延期されます。
Androidでこれを達成することは可能ですか?
ボタンの画像があります。それを利用するには、状態ごとに 1 つの追加画像が必要です: 1. 無効 2. 選択済み 3. 押されたなど..
iOS では、これらの追加の状態はすべて自動的に処理され、提供された元の画像から延期されます。
Androidでこれを達成することは可能ですか?
Androidでこれを達成することは可能ですか?
いいえ、そうではありません。セレクターを定義するには、すべての状態の画像が必要です
button_selector.xml を定義できます
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected state -->
<item android:drawable="@drawable/button_bg_selected" android:state_selected="true"></item>
<!-- Pressed state -->
<item android:drawable="@drawable/button_bg_pressed" android:state_pressed="true"></item>
<!-- Disabled state -->
<item android:drawable="@drawable/button_bg_disabled" android:state_enabled="false"></item>
<!-- Normal state -->
<item android:drawable="@drawable/button_bg_normal"></item>
</selector>
background
次に、このセレクターをボタンのとして割り当てるだけです
<Button
android:id="@+id/button1"
android:background="@drawable/button_selector"
android:layout_width="200dp"
android:layout_height="126dp"
android:text="Hello" />
参照:ボタンセレクター
お役に立てば幸いですツ</p>
選択して押された場合、ボタンの背景にルートを持つxmlファイルを使用できますが、無効になっている場合はJavaコードで処理する必要があると思います。無効状態についてはわかりません。
私が知る限り、cursor と呼ばれる新しいリソース ファイルでこの状態を処理する必要があります。
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/edittext_selector" android:layout_height="fill_parent"
android:layout_width="fill_parent">
<!-- Image display in background in select state -->
<item
android:state_pressed="true"
android:drawable="@drawable/edittextback1">
</item>
<!-- Image display in background in select state -->
<item
android:state_enabled="true"
android:state_focused="true"
android:drawable="@drawable/edittextback2">
</item>
<!-- Default state -->
<!--<item android:state_enabled="true"-->
<!--android:drawable="@drawable/your_ninepath_image">-->
<!--</item>-->
</selector>
この回答は、Android でボタンの状態を処理する方法を示し
ていres > layout
ます
例えば:
レイアウトファイル名がbtn.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/btn_pressed" />
<item android:drawable="@drawable/btn_normal"/>
</selector>
を設定できBackgroundResource
ますButton
yourButton.setBackgroundResource(R.layout.btn);
編集
さらに、質問への回答に近いこのリンクを参照できます
Android用のセレクターを使用する必要があります。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/focused"
android:state_focused="true" />
<item android:drawable="@drawable/normal" />
</selector>
android:background="@drawable/selector_button" />