3

ボタンのファミリーを作成したい。色を除いて、それらはすべて同じように見えます。これを行う強引な方法は、レイアウトをコピーして N 個のファイルに貼り付け、それぞれの色を変更することです。

これを達成するためのよりエレガントな方法は何ですか? @color/round_green_button_pressed_background以下のサンプル コードでは、との代わりに別の色を使用したいと考えて@color/round_green_button_unpressed_backgroundいますが、他のすべては 1 つの場所にとどまることが理想的です。

これをプログラムで行うのは、ファイルをコピーして貼り付けるよりもさらに悪いように思われるため、純粋な 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