2013-05-02 7 views
9

カスタムdrawable ressourceのButtonからパディングを変更してセレクタで再利用しようとしています。 描画可能に変更しても問題ないようだが、アプリが常に異なるドローアブルからaviable最高のパディングを使用します。Selector DrawableのPaddingを変更する

button_inactive.xml

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

<item> 
    <shape android:shape="rectangle" > 
     <solid android:color="#FF4d681f" /> 
    </shape> 
</item> 
<item 
    android:bottom="2dp" 
    android:left="2dp" 
    android:right="2dp" 
    android:top="2dp"> 
    <shape android:shape="rectangle" > 
     <solid android:color="#FFFFFFFF" /> 

     <padding 
      android:bottom="5dp" 
      android:top="5dp" /> 
    </shape> 
</item> 

button_active.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
<item> 
    <shape android:shape="rectangle" > 
     <solid android:color="#FF4D681F" /> 
    </shape> 
</item> 
<item 
    android:bottom="2dp" 
    android:left="2dp" 
    android:right="2dp" 
    android:top="2dp"> 
    <shape android:shape="rectangle" > 
     <solid android:color="#FF8da32d" /> 

     <padding 
      android:bottom="10dp" 
      android:top="10dp" /> 
    </shape> 
</item> 

button_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/button_active"/> 
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/button_active"/> 
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/button_active"/> 
<item android:drawable="@drawable/button_inactive"/> 

問題は、私のボタンが「非アクティブ」状態を含むすべての状態で10dpのパディングを使用していることです。

XMLでのみ動作するようにするにはソリューションがありますか、またはカスタムボタンをプログラム的に作成する方が良いですか?

+0

私はドロウアブルボタンで試してみました。通常のものにはパディングがありません。そして、そのパディングにパディングを追加しました。通常のものを表示していても、押したものからのパディングを使用します。私は同じ方法で使用している.pngファイルにも問題があります。私は、プレスされたものを普通のものより大きくしましたが、サイズは変わりません。これは吸う。 –

+0

http://stackoverflow.com/a/19227057/1645319 –

答えて

4

この

<solid android:color="#ffffff" /> 

<stroke 
    android:width="2dp" 
    android:color="@android:color/black" /> 

<padding 
    android:bottom="10dp" 
    android:left="10dp" 
    android:right="10dp" 
    android:top="10dp" /> 

<corners 
    android:bottomLeftRadius="6dp" 
    android:bottomRightRadius="6dp" 
    android:topLeftRadius="6dp" 
    android:topRightRadius="6dp" /> 

3

があなたのセレクタにandroid:variablePadding="true"属性を追加してみてください。

レイヤリストを使用しているときにどのように動作しているのかわかりませんが、答えを探している人は(私のように)誰とでも共有することに決めました。

+0

android:variablePaddingはバグのアトリビュートで、一度も働いたことがありません。 – ElSajko