Zhe*_*Liu 12 android android-layout android-view android-selector android-drawable
我有几个按钮,我需要一个椭圆形的边框.
所以我在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>
Run Code Online (Sandbox Code Playgroud)
我会android:background="@drawable/capsule_border.xml在需要它的地方使用它.
现在,我希望有一个按钮,以获得这个椭圆形边框,但也有一个android:background="?selectableItemBackground"视觉反馈.
我尝试使用带有selectableItembackground的父布局和带有capsule_border的按钮.但似乎突出显示的可点击区域是整个方块.而不仅仅是胶囊边界内的区域.
有没有我可以做到这一点,以便selectableItemBackground不高度视图的整个矩形,但只在我绘制的边框内?
azi*_*ian 18
有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>
Run Code Online (Sandbox Code Playgroud)
和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>
Run Code Online (Sandbox Code Playgroud)
和按钮:
<Button
android:background="@drawable/my_ripple"
... />
Run Code Online (Sandbox Code Playgroud)
会导致这个:
看到这篇文章.
我有第一个答案的更简单版本:
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?colorControlHighlight"> <!-- default ripple color -->
<item>
<!-- the background shape when it's not being clicked -->
<shape android:shape="rectangle">
<solid android:color="@color/colorPrimary" />
<corners android:radius="32dp" />
</shape>
</item>
</ripple>
Run Code Online (Sandbox Code Playgroud)
只需将其用作背景,但如果适用于以下情况,请记住删除阴影Button:
style="?borderlessButtonStyle"
祝你好运!
2020 年,这里有一个更简单的解决方案(API >= 21,因为我们使用的是?attr语法):
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?colorControlHighlight">
<!-- the ripple's color (1) -->
<!-- no `id` so our <shape> will be drawn and not just used as mask -->
<item>
<shape>
<corners android:radius="9dp" />
<solid android:color="@color/white" />
</shape>
</item>
</ripple>
Run Code Online (Sandbox Code Playgroud)
(1)如果您没有colorControlHighlight在主题中进行覆盖,则波纹颜色将是 Android 的默认颜色。如果您确实?android:colorControlHighlight覆盖了它但仍想使用 Android 的默认使用