android - 引用具有 alpha 属性的颜色文件时,形状 XML 中的渐变颜色显示错误
问题描述
新建一个项目,只将主activity的背景改为shape梯度xml文件,这里的梯度android:centerColor
是指?attr/colorAccent
带alpha值的。问题出现了。无论你设置什么颜色,它都会变成粉红色
但是如果android:centerColor
梯度中的值是硬编码,就不会出现问题。
的编辑器界面mask.xml
可以正常显示,但是在设备或AVD上运行时中间的颜色变成粉红色。
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:background="@drawable/mask"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
@drawable/mask.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="90"
android:centerColor="@color/alpha_95"
android:endColor="?attr/colorOnPrimary"
android:startColor="?attr/colorOnPrimary"
android:type="linear" />
</shape>
@color/alpha_95.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="0.95" android:color="?attr/colorAccent" />
</selector>
解决方案
推荐阅读
- c++ - 为什么我们不能从派生类中调用重载函数
- python - ImportError:无法导入“rest_framework_simplejwt.authentication.JWTAuthentication”
- vue.js - vuetify数据表分页自定义功能
- cookies - 如何在 jmeter 中处理 ex _ga 的谷歌分析 cookie
- java - 将带有对象数组的 JSON 转换为正确的 Java 对象
- android - 根据 if 条件为菜单图标着色
- c# - 如何使用 Uipath 数据库活动将数据表插入 SQL Server 数据库?
- weblogic12c - Weblogic 12C外部属性文件未加载
- r - 使用混合的随机和确定性输入编码集成贝叶斯模型
- hyperledger-fabric - 如何防止第三方在 Hyperledger Fabric 中看到同一通道上的数据?