android - 带有背景图像的按钮和使用 Material 设计风格 - Android

标签 android button material-design

样式不工作的 Material 设计按钮

我有一个背景颜色为深蓝色的按钮,我需要该按钮的 Material 设计风格,如下所示:

怎么可能

    <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:background="#0040FF"
        android:textColor="#FFFF"
        android:layout_marginRight="10dp"
        android:text="Login" />

我需要使用:

style="?android:attr/buttonStyleSmall"
android:background="?android:attr/selectableItemBackground"

通常我用的时候

<Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:paddingRight="15dp"
            android:layout_gravity="center"
            android:text="Login"
            android:paddingLeft="15dp"
            android:id="@+id/login" />

它与设计一起工作。

是否可以使用背景图像/颜色执行 Material 设计按钮样式。 抱歉我在 UI 中工作的简单问题。

最佳答案

在 drawable-v21 目录中使用 drawable ripple.xml 作为背景,例如:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="?attr/colorControlHighlight">
    <item android:drawable="?attr/colorPrimary"/>
</ripple>

对于低于 sdk 21 的版本,您必须在 drawable 文件夹中创建一个自定义选择器 ripple.xml。然后它将加载到 pre lollypop 设备中

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/primaryPressed" android:state_pressed="true"/>
    <item android:drawable="@color/primaryFocused" android:state_focused="true"/>
    <item android:drawable="@color/primary"/>
</selector>

根据需要使用自定义颜色

然后

<Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:paddingRight="15dp"
            android:layout_gravity="center"
            android:text="Login"
            android:paddingLeft="15dp"
             android:background="@drawable/ripple"
            android:id="@+id/login" />

关于android - 带有背景图像的按钮和使用 Material 设计风格 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33754151/

相关文章:

java - 围绕 RelativeView 的中心椭圆移动 ImageView

android - 读取链接 ('/proc/self/fd/21' ) 失败 : Permission denied [fd=21] error in Android 6 after adding Admob module to Appcelerator app

android - "Failed to load map. Could not contact Google servers."

java - 从相似歌曲之间的字符串中获取字符

javascript - 通过不同的按钮调用具有不同参数的函数 - JavaScript

html - 链接和按钮定位

java - GWT 用左键和右键移动按钮

javascript - 降低高度 Material 文本字段

javascript - 下拉列表中的登录表单( Material 设计精简版)

javascript - Vuetify 中是否有特定的数字输入组件?