android - xml中的渐变不会出现在按钮上
问题描述
我在我的可绘制资源文件 (.xml) 中创建了一个渐变,然后将其连接到按钮的(在 activity_main.xml 中)背景。不幸的是,这个文件中的所有结构都没有出现在这个按钮上。我是初学者。这是代码问题还是其他问题?渐变可以用作 ImageView 甚至可以用作 Switch,但不能用作按钮的背景。gradient.xml 中的代码:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<item>
<shape>
<gradient
android:angle="90"
android:endColor="#b5b6d2"
android:startColor="#555994"
android:type="linear" />
<corners
android:radius="0dp"/>
<padding
android:right="10dp"
android:top="10dp"
android:bottom="10dp"
android:left="10dp"/>
</shape>
</item>
</selector>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:background="@drawable/gradient"
android:text="@string/button"
android:textColor="@android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
解决方案
当您想根据小部件的不同状态显示不同的背景/颜色时,您可以使用选择器。对于您的情况,用您的可绘制文件中的代码替换您的代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="#b5b6d2"
android:startColor="#555994"
android:type="linear" />
<corners
android:radius="0dp"/>
</shape>
更正: Button 小部件将 colorAccent 作为默认背景奇怪地将小部件更改为 AppCompatButton ,它将解决问题。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:text="Button"
android:background="@drawable/gradient"
android:textColor="@android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
推荐阅读
- database - 如何编辑数据库中存在数据的数据?在代码点火器 3 上
- android - React Native - Play 商店警告“从 5 月 5 日开始,您必须让我们知道为什么您的应用需要广泛的存储访问权限”
- c - 如何在C语言的另一个函数中使用局部变量(未声明的标识符)(作用域规则)
- java - 点击按钮时谷歌地图崩溃。(无效 com.google.maps.api.android.lib6.impl.bp.v())
- c++ - 提取 .txt 文件中的特定数据列
- python - 在 XGBoost 中使用 MAPE 作为评估指标
- r - Facet_wrap标签作为ggplot中的面板标签
- angular - 无法从 Angular 11 中的 httpCilent 模块将数据分配给类变量(例如,在 http.get().subscribe(...) 内部)
- firebase - Firebase 应用内消息 - 针对特定用户受众的活动存在问题
- android - 根据宽度对齐多个标签