-1

現在、私はこのセレクターを使用しています:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true"
   android:drawable="@color/green" /> <!-- pressed -->
  <item android:state_focused="true"
   android:drawable="@color/green" /> <!-- focused -->
</selector>

ボタンが押されておらず、フォーカスされていない場合に備えて、ボタンのデフォルトのドローアブルを設定したいと思います。これを行うには、セレクターをどのように変更すればよいですか?

4

2 に答える 2

0

res/values/colors.xmlファイルで、次のようにさまざまな状態の異なる色を定義します (色には独自の 16 進コードを使用してください)。

<color name="green_pressed">#ff00f000</color>
<color name="green_focused">#ff00f700</color>
<color name="green_default">#ff00ff00</color>

さまざまな状態のさまざまなドローアブルを宣言する

button_focused_green.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="@color/green_focused" />
    <!-- optional, remove if you don't want round border -->
    <corners android:radius="4dp" />
</shape>

button_pressed_green.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="@color/green_pressed" />
    <!-- optional, remove if you don't want round border -->
    <corners android:radius="4dp" />
</shape>

button_default_green.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="@color/green_default" />
    <!-- optional, remove if you don't want round border -->
    <corners android:radius="4dp" />
</shape>

StateListDrawableに適用される xml ファイルで宣言します。Button

button_green.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/button_focused_green" android:state_focused="true"/>
    <item android:drawable="@drawable/button_pressed_green" android:state_pressed="true"/>        
    <item android:drawable="@drawable/button_default_green"/>

</selector>

Buttonレイアウト xml でbackground 属性を設定します

<Button 
    ...
    android:background="@drawable/button_green"
.../>

お役に立てれば。

于 2013-12-27T11:44:51.053 に答える
0

これを試して:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item android:drawable="@drawable/button" 
        android:state_focused="false" 
        android:state_pressed="false" 
        android:state_selected="false"/>
    <!-- Pressed -->
    <item android:drawable="@drawable/button_pressed" 
        android:state_focused="false" 
        android:state_pressed="true" 
        android:state_selected="false"/>
</selector>
于 2013-12-27T11:09:44.023 に答える