将按钮外观与焦点颜色相结合



>我有一个问题。我有 2 个 xml 文件:第一个在聚焦按钮并按下按钮时更改颜色:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true" >
    <shape>
      <solid android:color="#318e3e" />
    </shape>
  </item>
  <item android:state_focused="true" >
    <shape>
      <solid android:color="#318e3e" />
    </shape>
  </item>
  <item>
    <shape>
      <solid android:color="#24c139" />
    </shape>
  </item>
</selector>

第二个只是改变了按钮的外观:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle" >
  <solid android:color="#28bf3c" />
  <stroke android:width="1.5dp" android:color="#000000"/>
  <corners android:radius="25dp"/>

  <item android:state_pressed="true" >
    <shape>
      <solid android:color="#1b7196" />
    </shape>
  </item>
  <item android:state_focused="true" >
    <shape>
      <solid android:color="#1b7196" />
    </shape>
  </item>
  <item>
    <shape>
      <solid android:color="#28bf3c" />
    </shape>
  </item>
</shape>

但是如何将这两个文件合并为一个文件?

您可以在一个XML中执行此操作(根据您的需要的颜色(:

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_window_focused="false">
    <shape android:shape="rectangle">
      <corners android:radius="25dp" />
      <solid android:color="#24c139" />
      <stroke android:width="1.5dp" android:color="#000" />
    </shape>
  </item>
  <item android:state_pressed="true">
    <shape android:shape="rectangle">
      <corners android:radius="25dp" />
      <solid android:color="#318e3e" />
      <stroke android:width="1.5dp" android:color="#000" />
    </shape>
  </item>
</selector>

最新更新