2015-02-09 65 views
0

我想创建一个ImageButton可绘制资源文件,该文件同时具有图像(不能展开为背景),彩色背景和右下边框。此外,我希望有不同的设置state_pressed,state_selected和默认状态。带图像图标,彩色背景和边框的ImageButton

现在,我已经成功地创建该图像的XML资源文件的所有状态:

<item android:state_selected="true" android:drawable="@drawable/emailselected" /> 
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/emailselected" /> 
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/emailselected" /> 
<item android:state_focused="true" android:drawable="@drawable/emailseleted" /> 
<item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/email" /> 

我也有背景,右侧和底部边框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="@color/darkgray" /> 
     </shape> 
    </item> 

    <item 
     android:bottom="1dp" 
     android:right="1dp"> 
     <shape android:shape="rectangle"> 
      <solid android:color="@color/orange" /> 
     </shape> 
    </item> 

    <item 
     android:bottom="1dp" 
     android:right="1dp"> 
     <shape android:shape="rectangle"> 
      <solid android:color="@color/gray" /> 
     </shape> 
    </item> 
</layer-list> 

我想将所有这些结合在一个资源文件中。可能吗? 所以基本上我想:

  • 按下按钮:背景将变为蓝色和图标“email_selected”

  • 按钮选择:背景将变为蓝色和图标“email_selected”

  • 默认:背景变成灰色和图标是“邮件”

我已经尽力了TW结合o通过在背景颜色为“橙色”和“灰色”的项目中使用“android:state”的资源文件,但它不起作用。

任何人都可以请帮忙吗?

谢谢。

回答

0

那么,我不得不使用单独的背景资源文件,并使用状态按下和选择项目的选择器。我猜你不能在同一个资源文件中有“背景”和“可绘制”。这里是我的代码:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_selected="true"> 
    <layer-list> 
     <item> 
      <shape android:shape="rectangle"> 
       <solid android:color="@color/darkgray" /> 
      </shape> 
     </item> 

     <item android:state_focused="false" android:state_pressed="false" android:bottom="1dp" android:right="1dp"> 
      <shape android:shape="rectangle"> 
       <solid android:color="@color/orange" /> 
      </shape> 
     </item> 
    </layer-list> 
</item> 
<item android:state_focused="true" android:state_pressed="true"> 
    <layer-list> 
     <item> 
      <shape android:shape="rectangle"> 
       <solid android:color="@color/darkgray" /> 
      </shape> 
     </item> 

     <item android:state_focused="false" android:state_pressed="false" android:bottom="1dp" android:right="1dp"> 
      <shape android:shape="rectangle"> 
       <solid android:color="@color/orange" /> 
      </shape> 
     </item> 
    </layer-list> 
</item> 
<item android:state_focused="false" android:state_pressed="true"> 
    <layer-list> 
     <item> 
      <shape android:shape="rectangle"> 
       <solid android:color="@color/darkgray" /> 
      </shape> 
     </item> 

     <item android:state_focused="false" android:state_pressed="false" android:bottom="1dp" android:right="1dp"> 
      <shape android:shape="rectangle"> 
       <solid android:color="@color/orange" /> 
      </shape> 
     </item> 
    </layer-list> 
</item> 
<item android:state_focused="true"> 
    <layer-list> 
     <item> 
      <shape android:shape="rectangle"> 
       <solid android:color="@color/darkgray" /> 
      </shape> 
     </item> 

     <item android:state_focused="false" android:state_pressed="false" android:bottom="1dp" android:right="1dp"> 
      <shape android:shape="rectangle"> 
       <solid android:color="@color/orange" /> 
      </shape> 
     </item> 
    </layer-list> 
</item> 
<item android:state_focused="false" android:state_pressed="false"> 
    <layer-list> 
     <item> 
      <shape android:shape="rectangle"> 
       <solid android:color="@color/darkgray" /> 
      </shape> 
     </item> 

     <item android:state_focused="false" android:state_pressed="false" android:bottom="1dp" android:right="1dp"> 
      <shape android:shape="rectangle"> 
       <solid android:color="@color/gray" /> 
      </shape> 
     </item> 
    </layer-list> 
</item>