【发布时间】:2016-09-14 07:12:29
【问题描述】:
我想创建具有自定义形状的按钮,它会像主题 "Base.Widget.AppCompat.Button.Colored".
但是因为我必须自定义它的形状(圆角我必须覆盖它的android:background - 这是迄今为止我知道的唯一方法(不......不,我不会对 FrameLayout 使用肮脏的黑客)。
目前,只能通过在 xml 文件中提供我们自定义的 <shape> 作为背景可绘制对象来完成。
启用 selectableItemBackground 的最有希望的代码是:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="@color/colorAccent"/>
<corners android:topLeftRadius="@dimen/button_shape_radius"
android:topRightRadius="@dimen/button_shape_radius"/>
</shape>
</item>
<item android:drawable="?attr/selectableItemBackground">
<shape>
<solid/>
<corners android:topLeftRadius="@dimen/button_shape_radius"
android:topRightRadius="@dimen/button_shape_radius"/>
</shape>
</item>
</layer-list>
不幸的是,我无法用<item android:drawable="?attr/selectableItemBackground"> 塑造第二个项目,因此最终按下项目的形状是矩形。
如果有人会给我这个问题的解决方案,我会很感激。 我使用 API_MIN = 16,所以不能使用涟漪效应。我也不想使用 FrameLayout 或其他外部库,迫使我用一些东西包装 Button。
【问题讨论】:
标签: android shape android-selector