【问题标题】:Android studio - gradient background & strokeAndroid studio - 渐变背景和描边
【发布时间】:2020-08-27 12:30:36
【问题描述】:

我试图给我的按钮一个渐变背景和更强的笔触。我已经阅读/看过很多教程,但对我没有任何帮助。

这是我的插件 .xml 文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:shape="rectangle">

    <gradient android:startColor="#25AAFF"
        android:endColor="#004F81">
    </gradient>

    <stroke
        android:width="5dp"
        android:color="#000000">
    </stroke>

    <corners android:radius="5dp"></corners>

注意:角落工作正常

和按钮文件

     <Button
        android:background="@drawable/login_button"
        android:id="@+id/login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/login_password_input"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="@string/login_button_text"
        android:textColor="#FFFFFF"
        android:textStyle="bold">
    </Button>

【问题讨论】:

  • 你在使用材料组件库吗?

标签: java android android-button material-components-android material-components


【解决方案1】:

使用材质组件库,&lt;Button /&gt;&lt;com.google.android.material.button.MaterialButton /&gt; 之间没有区别。更多信息请查看this post

MaterialButton 忽略 android:background 直到发布1.2.0-alpha06。 在此版本中,您可以使用类似以下内容的渐变背景:

<Button
    android:background="@drawable/bg_button_gradient"
    app:backgroundTint="@null"
    ... />

您还可以使用app:cornerRadiusapp:strokeWidthapp:strokeColor 属性应用笔触:

<Button
    app:cornerRadius="4dp"
    app:strokeWidth="1dp"
    app:strokeColor="@color/primaryLightColor"
    ..>

如果您使用的是早期版本的材料组件库,您可以使用AppCompatButton

<androidx.appcompat.widget.AppCompatButton
    android:background="@drawable/bg_button_gradient"
    ..>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多