android - 如何创建具有高度的android按钮背景svg?
问题描述
我一直在尝试实现像附加图像中的按钮,为此我创建了带有阴影的渐变 SVG,使其看起来像高程,但是当这个 svg 应用于按钮作为背景可绘制时,它看起来很平坦,波纹效果也消失了。
这是我的代码:
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="35dp"
android:enabled="false"
android:elevation="10dp"
android:translationZ="10dp"
android:stateListAnimator="@null"
android:background="@drawable/bg_gradient"
android:text="@string/btn_txt_login"
android:textColor="@android:color/white"
android:textSize="18sp" />
我正在使用 SVG 文件创建带有圆角边框的背景渐变。任何帮助,将不胜感激。
解决方案
我有一个解决方案给你。
将此添加到您的 build.gradle(Module:app) 依赖项中:
implementation 'com.android.support:cardview-v7:27.1.1'
现在使用 CardView 布局而不是 Button:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="4dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center_vertical|center_horizontal"
android:textSize="18sp"
android:text="LOGIN"
android:textColor="@android:color/white"
android:textStyle="bold"
android:background="@drawable/gradient"/>
</android.support.v7.widget.CardView>
在 drawable 文件夹中创建 gradient.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#8b0ab7"
android:endColor="#0b85d0"
android:angle="0"/>
<corners android:radius="10dp"/>
</shape>
推荐阅读
- php - PHP SOAP 请求失败
- android - TypeError:无法读取未定义 [react-native-payments] 的属性“显示”
- selenium - ModHeader Chrome 扩展与机器人框架
- pyspark - 如何在pyspark中求和?
- spring - 反向代理背后的 Spring Security SAML
- mysql - 从选择和变量插入MYSQL
- android - 带有扩展方法的 Kotlin 数据绑定
- arrays - 通过解析 systemd-resolve 输出生成 YAML
- android - “允许创建”不适用于 Firestore 规则中的某些集合
- r - 使用 element_blank 时向 ggplot 添加实验室