【问题标题】:Shaped drawable with selectableItemBackground as background以 selectableItemBackground 作为背景的形状可绘制对象
【发布时间】:2017-04-07 17:53:40
【问题描述】:

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

所以我在 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 不在视图的整个矩形范围内,而只在我绘制的边框内?

【问题讨论】:

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


    【解决方案1】:

    拥有 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 文章。

    【讨论】:

    • 真的很不错。如果有
    • 为此祝福你!
    【解决方案2】:

    我有第一个答案的更简单的版本:

    <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>
    

    只需将其用作背景,但如果适用于Button,请记住REMOVE THE SHADOW

    style="?borderlessButtonStyle"

    祝你好运!


    【讨论】:

    • 此解决方案的最低安卓版本是多少?
    • @Zhen Liu 我没有看到任何 API 级别的要求,我猜对所有版本都有好处。
    【解决方案3】:

    这是 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>
    

    (1) 如果您没有在主题中覆盖colorControlHighlight,波纹的颜色将是 Android 的默认颜色。如果您确实覆盖它但仍想使用 Android 的默认设置,请改用 ?android:colorControlHighlight

    【讨论】:

    • 这种方法有效,但它有一个问题,即在未单击项目时它总是绘制背景颜色(在这种情况下为白色)。这会导致透支。另一方面,使用蒙版我们可以有一个默认的透明背景并避免过度绘制。
    • @flaw 只是删除实线并使用 backgroundTint。由于新的系统连锁效应,这应该是 2021 年的第一答案
    • 2021 年更好的解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 2013-03-25
    相关资源
    最近更新 更多