参数化XML drawable

时间:2014-05-16 15:45:58

标签: android layout android-drawable statelistdrawable

我想创建一系列按钮。除了颜色外,它们看起来都一样。这样做的蛮力方法是将布局复制/粘贴到N个文件中,然后更改每个文件中的颜色。

实现这一目标的更优雅方式是什么?在下面的示例代码中,我想使用不同的颜色而不是@color/round_green_button_pressed_background@color/round_green_button_unpressed_background,但其他所有颜色应该理想地保留在一个位置。

以编程方式执行此操作似乎比复制/粘贴文件更糟糕,所以希望有一个涉及纯XML基础布局的答案。

示例代码:button_green.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true">
    <shape>
      <size android:width="120dp" android:height="120dp"/>
      <solid android:color="@color/round_green_button_pressed_background"/>
    </shape>
  </item>
  <item>
    <shape>
      <size android:width="120dp" android:height="120dp"/>
      <solid android:color="@color/round_green_button_unpressed_background"/>
    </shape>
  </item>
</selector>

0 个答案:

没有答案