- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 values/colors.xml
中有以下颜色:
<color name="grey_1">#0F0E10</color>
我想在渐变中引用这个颜色:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="linear"
android:angle="-90"
android:startColor="#000F0E10"
android:endColor="#990F0E10"/>
</shape>
但是,这复制了 RGB 颜色定义。理想情况下,我想写这样的东西:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="linear"
android:angle="-90"
android:startColor="alpha(00, @color/grey_1)"
android:endColor="alpha(99, @color/grey_1)"/>
</shape>
或者这个:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="linear"
android:angle="-90"
android:startColor="@color/grey_1"
android:startTransparency="#00"
android:endColor="@color/grey_1"
android:endTransparency="#99"/>
</shape>
这可能吗?
最佳答案
您可以在 API 23 或更高版本中使用 ColorStateList
执行此操作.
来自文档:
Starting with API 23, items may optionally define an android:alpha attribute to modify the base color's opacity. This attribute takes a either floating-point value between 0 and 1 or a theme attribute that resolves as such. The item's overall color is calculated by multiplying by the base color's alpha channel by the alpha value. For example, the following item represents the theme's accent color at 50% opacity:
<item android:state_enabled="false"
android:color="?android:attr/colorAccent"
android:alpha="0.5" />
因此,就我而言,我会这样做:
color/gradient_start_color.xml
:
<item android:color="@color/grey_1"
android:alpha="0" />
color/gradient_end_color.xml
:
<item android:color="@color/grey_1"
android:alpha="0.6" />
drawable/gradient.xml
:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="linear"
android:angle="-90"
android:startColor="@color/gradient_start_color"
android:endColor="@color/gradient_end_color" />
</shape>
关于android - 如何将 alpha channel 添加到 xml 中的现有 Android 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53163354/
我是一名优秀的程序员,十分优秀!