我有一个具有 3 种状态(按下、选择和默认)的自定义可绘制对象。在 Android Studio 预览中切换状态时,按下状态不会触发对可绘制对象的任何更改。选定的状态按预期工作。我在专注状态下也经历过这种行为, state_checked 似乎正在工作。在我为测试目的创建的另一个绘图中,一切都按预期工作,即使状态的顺序完全相同。
具有错误行为的自定义可绘制对象:
<item android:state_pressed="true">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/sev_ripple_color">
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/white" />
<corners android:radius="10dp" />
</shape>
</item>
</ripple>
</item>
<item>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/sev_ripple_color">
<item>
<shape android:shape="rectangle">
<padding android:bottom="@dimen/sev_margin_xsmall"
android:top="@dimen/sev_margin_xsmall"
android:left="@dimen/sev_margin_xsmall"
android:right="@dimen/sev_margin_xsmall" />
<stroke
android:width="2dp"
android:color="@color/sev_btn_color_primary" />
<solid android:color="@android:color/white" />
<corners android:radius="10dp" />
</shape>
</item>
</ripple>
</item>
<item android:drawable="@drawable/ic_sev_male" android:top="20dp" android:bottom="20dp" android:start="30dp" android:end="30dp" android:gravity="center">
</item>
</layer-list>
</item>
<item android:state_selected="true">
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/sev_ripple_color">
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/white" />
<corners android:radius="10dp" />
</shape>
</item>
</ripple>
</item>
<item>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/sev_ripple_color">
<item>
<shape android:shape="rectangle">
<padding android:bottom="@dimen/sev_margin_xsmall"
android:top="@dimen/sev_margin_xsmall"
android:left="@dimen/sev_margin_xsmall"
android:right="@dimen/sev_margin_xsmall" />
<stroke
android:width="2dp"
android:color="@color/sev_btn_color_primary" />
<solid android:color="@android:color/white" />
<corners android:radius="10dp" />
</shape>
</item>
</ripple>
</item>
<item android:drawable="@drawable/ic_sev_male" android:top="20dp" android:bottom="20dp" android:start="30dp" android:end="30dp" android:gravity="center">
</item>
</layer-list>
</item>
<item>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/sev_ripple_color">
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/white" />
<corners android:radius="10dp" />
</shape>
</item>
</ripple>
</item>
<item>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/sev_ripple_color">
<item>
<shape android:shape="rectangle">
<padding android:bottom="@dimen/sev_margin_xsmall"
android:top="@dimen/sev_margin_xsmall"
android:left="@dimen/sev_margin_xsmall"
android:right="@dimen/sev_margin_xsmall" />
<stroke
android:width="2dp"
android:color="#CCCCCC" />
<solid android:color="@android:color/white" />
<corners android:radius="10dp" />
</shape>
</item>
</ripple>
</item>
<item android:drawable="@drawable/ic_sev_male" android:top="20dp" android:bottom="20dp" android:start="30dp" android:end="30dp" android:gravity="center">
</item>
</layer-list>
</item>
具有相同顺序和正确行为的自定义可绘制对象:
<!-- TODO Redundanz prüfen, ändert sich ja nur checkbox state -->
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="@android:color/holo_blue_bright" />
</shape>
</item>
<item android:state_selected="true">
<shape android:shape="rectangle">
<solid android:color="@android:color/white" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/red" />
</shape>
</item>