3

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

实现这一目标的更优雅的方法是什么?在下面的示例代码中,我想使用不同的颜色而不是@color/round_green_button_pressed_backgroundand @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>
4

0 回答 0