android - 以 selectableItemBackground 作为背景的形状可绘制对象

标签 android android-layout android-view android-drawable android-selector

我有几个按钮需要椭圆形边框。

所以我在 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 的按钮。但似乎突出显示的可点击区域是整个正方形。而不仅仅是胶囊边界内的区域。

enter image description here

有什么方法可以让 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"
    ... />

将导致:

enter image description here

参见 this文章。

关于android - 以 selectableItemBackground 作为背景的形状可绘制对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43284336/

相关文章:

android - 如何在android 3.0中启动 Action 模式

android - 在复合 View 小部件上保存状态

android - 为什么我添加 "android:id"时我的 android 应用程序崩溃?

java - Android - 动态构建 View block 并将其附加到布局

android - 无法从 Play Store Production Area 停用 Android 分阶段发布应用程序

java - Android中捕获线程中的所有异常

android - 更改 SearchView 返回图标颜色

android - 将按钮的文本对齐到底部

Android:setOnLongClickListener() 不可靠

android - 房间 : how to convert to SQLite method to Dao's @Query