我有几个按钮需要椭圆形边框。
所以我在 capsule_border.xml 中有这个
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="9999dp"/>
<stroke
android:width="1px"
android:color="@color/border_gray" />
</shape>
我会在需要的地方使用 android:background="@drawable/capsule_border.xml
。
现在,我想要一个具有椭圆形边框的按钮,还有一个用于视觉反馈的 android:background="?selectableItemBackground"
。
我尝试使用带有 selectableItembackground 的父布局和带有 capsule_border 的按钮。但似乎突出显示的可点击区域是整个正方形。而不仅仅是胶囊边界内的区域。
有什么方法可以让 selectableItemBackground 不高度覆盖 View 的整个矩形,而是只在我绘制的边框内?
最佳答案
拥有round_corners.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/transparent"/>
<corners android:radius="15dp" />
<stroke
android:width="1px"
android:color="#000000" />
</shape>
和my_ripple.xml:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:attr/colorControlHighlight">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#000000" />
<corners android:radius="15dp" />
</shape>
</item>
<item android:drawable="@drawable/round_corners" />
</ripple>
和按钮:
<Button
android:background="@drawable/my_ripple"
... />
将导致:
参见 this文章。
关于android - 以 selectableItemBackground 作为背景的形状可绘制对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43284336/