2011-12-01 9 views
42

レイヤリストをボタンのドロアブルとして使用するにはどうすればいいですか? 私がボタンを持っている: Androidボタンリストのレイヤリストの使用

<item android:state_pressed="true"> 
    <shape> 
     <gradient android:endColor="@color/white" 
      android:startColor="@color/grey_blue_light" android:angle="90" /> 
     <stroke android:width="1dp" android:color="@color/aqua_blue" /> 
     <corners android:radius="3dp" /> 
     <padding android:left="10dp" android:top="10dp" 
      android:right="10dp" android:bottom="10dp" /> 
    </shape> 
</item> 


<item android:state_focused="true"> 

</item> 
<item> 

</item> 

今、私はボタンの状態が押されていると言う形として使用される層-リストを必要とする:どのように

<?xml version="1.0" encoding="UTF-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item> 
    <shape android:shape="oval"> 
     <solid android:color="@color/aqua_blue" /> 
    </shape> 
</item> 
<item android:top="1dp" android:left="1dp" android:right="1dp" android:bottom="1dp"> 
    <shape android:shape="oval"> 
     <solid android:color="@color/aqua_blue" /> 
    </shape> 
</item> 

このレイヤーリストをボタンセレクターで使用しますか?

答えて

114

ステップ-1 ボタンの3つの異なる状態のためのdrawableフォルダの下に3つの異なるlayer_list xmlを作成します。例えば、これらのXMLの名前はlayer1.xml, layer2.xml, layer3.xml

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

      <gradient 
       android:angle="270" 
       android:startColor="#0000ff" 
       android:endColor="#0000dd" 
       android:type="linear" 
       />  
     </shape> 
    </item> 

</layer-list> 

STEP-2 がbtn_background.xmlと命名セレクタXMLを作成して、描画可能な属性にlayer_list XMLを渡すある

<selector 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_pressed="true" android:drawable="@drawable/layer1"> 

    </item> 

    <item android:state_focused="true" android:drawable="@drawable/layer2"> 

    </item> 

    <item android:drawable="@drawable/layer3">   

    </item> 
</selector> 

ステップ3 ボタンの背景としてセレクタxmlを設定します。android:background="@drawable/btn_background"

+1

偉大な答え。しかし、あなたが ' 'の下に単一の' '要素を持っているので、後者は省略できると思います。私。 ''です。私のために働いた。 – SoftDesigner

0

タグをlayer-listと付けてください。すべて正常に動作します。

関連する問題