【问题标题】:Button with custom background and "?attr/selectableItemBackground"具有自定义背景和“?attr/selectableItemBackground”的按钮
【发布时间】:2017-03-13 11:10:01
【问题描述】:

如何定义具有自定义背景和属性
"?attr/selectableItemBackground"Button

使用此代码,"?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"/>

【问题讨论】:

  • 你可以改用rippledrawables。
  • 但我不想为 API>21 创建一个涟漪,为 API

标签: android material-design android-view android-button android-attributes


【解决方案1】:

好的,您可以选择parent

创建父级并将background 设置为白色并使用selectableItemBackground 作为子级的背景。

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

【讨论】:

  • 添加一个父布局只是为了给他的孩子一个背景颜色是个好习惯吗?
  • 不是一个好习惯,但既然你不想使用rippledrawable,它是一个选项。
【解决方案2】:

尝试设置

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 级别?
  • API 25,最小值为 15
  • 这里工作... 带有可点击、可聚焦、focusableInTouchMode、前景和背景标志的按钮,但我的主要布局没有背景或前景。我的应用主题是“默认”。你的布局主题是什么?
  • 用你的布局和按钮编辑你的答案,我会试试的。我的 AppTheme 是 Theme.AppCompat.Light.DarkActionBar。谢谢
【解决方案3】:

你应该为它写一个特殊的drawable

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

【讨论】:

  • 这一定是最值得推荐的答案。像魅力一样工作。
猜你喜欢
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-07
  • 2015-09-13
  • 2012-01-16
  • 1970-01-01
相关资源
最近更新 更多