【问题标题】:android ripple button with background带背景的android波纹按钮
【发布时间】:2015-02-03 01:30:04
【问题描述】:

我使用 XML 在 android 5.0 中成功创建了一个波纹按钮。我的代码如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- the flat button to be used in app -->
<!-- define the ripple -->
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <!-- the ripple object -->
    <item android:id="@android:id/mask">
        <!-- button shape (w/ radius) -->
        <shape android:shape="rectangle">
            <!-- radius of vertices -->
            <corners
                android:topLeftRadius="7dp"
                android:topRightRadius="7dp"
                android:bottomLeftRadius="7dp"
                android:bottomRightRadius="7dp" />
            <!-- color accent (not working) -->
            <solid android:color="?android:colorAccent" />
        </shape>
    </item>
</ripple>

我的按钮有问题:按钮的背景颜色不会改变。我也试过:

<solid android:color="#0000ff" />

背景仍然保持透明(colorAccent 也不起作用)。

如果不通过

,我应该如何将矩形(按钮)的背景设​​置为蓝色
<solid/>

财产?

谢谢

【问题讨论】:

  • 阅读 RippleDrawable 的文档。不绘制遮罩层。从 标记中删除 android:id 属性。

标签: android button android-5.0-lollipop rippledrawable


【解决方案1】:

使用@android:id/mask标识符将图层设置为蒙版,仅用于裁剪边界,不绘制。删除 id 属性应该可以为您提供所需的内容。此外,如果角的半径相同,则可以只使用android:radius 属性。

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="?android:attr/colorControlHighlight">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="7dp" />
            <solid android:color="?android:attr/colorAccent" />
        </shape>
    </item>
</ripple>

【讨论】:

  • 但是,如果对实体使用透明颜色,则不会显示波纹(这在使用非透明颜色作为笔划时很常见 - 就像在绘制轮廓形状时一样)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-13
  • 1970-01-01
  • 2017-02-21
  • 1970-01-01
  • 1970-01-01
  • 2019-01-13
相关资源
最近更新 更多