具有自定义背景和"?attr/selectableItemBackground"按钮

7
如何定义一个带有自定义背景的按钮并使用属性"?attr/selectableItemBackground"
使用此代码,属性"?attr/selectableItemBackground"将被忽略,触摸反馈不会显示。
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:foreground="?attr/selectableItemBackground"/>

使用另一段代码后,可选择性正常工作,但会丢失背景颜色:
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/selectableItemBackground"/>

2
您可以使用“ripple”可绘制对象。 - user2756345
但是我不想为API>21创建一个波浪和另一个API<21的可绘制对象。使用属性不可能吗? - MarcGV
3个回答

4

Alright you have an option of parent

Create parent and give background to white color and use selectableItemBackground as child's background.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/selectableItemBackground"/>

</LinearLayout>


1
给子元素添加一个父布局仅为了获得背景颜色是一个好的做法吗? - MarcGV
1
虽然这不是一个好的实践方法,但是既然您不想使用ripple可绘制效果,那么这是一个选择。 - user2756345

3

你应该为此编写一个特殊的可绘制对象

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <selector>
            <item android:drawable="@drawable/your_drawable_rounded_enabled" android:state_enabled="true"/>
            <item android:drawable="@drawable/your_drawable_rounded_disabled" android:state_enabled="false"/>
        </selector>
    </item>
    <item android:drawable="?attr/selectableItemBackground"/>
</layer-list>

这一定是最值得推荐的答案。运行得很顺畅。 - Makari Kevin

3
尝试设置
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"

示例主要布局活动:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.petercarlim.myapplication.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:background="@android:color/darker_gray"
        android:foreground="?android:attr/selectableItemBackground"/>

</LinearLayout>

或者在你的布局的父布局(调用此布局的其他布局)中进行设置:

    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true" 

API 等级是多少? - Peter R
API 25 和最低版本为 15。 - MarcGV
这里的工作是...按钮带有可点击、可聚焦、可触摸聚焦、前景和背景标志,但我的主要布局没有背景或前景。而且我的应用程序主题是“默认”的。你的布局主题是什么? - Peter R
请编辑您的答案并在其中添加布局和按钮,我会尝试它。我的AppTheme是Theme.AppCompat.Light.DarkActionBar。谢谢。 - MarcGV

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接