【问题标题】:gradient color in integer-array android整数数组android中的渐变颜色
【发布时间】:2017-06-29 08:01:35
【问题描述】:

我通过 color.xml 文件中的整数数组为我的卡片视图背景使用随机颜色

颜色.xml:

<item name="Gold" type="color">#FFFFD700</item>
<item name="Orange" type="color">#DSADSA</item>
<item name="LightSalmon" type="color">#EWQ5645W</item>

<integer-array name="androidcolors">
    <item>@color/Gold</item>
    <item>@color/Orange</item>
    <item>@color/LightSalmon</item>
</integer-array>

但我想为我的背景设置渐变色,我尝试了这个:

<integer-array name="androidcolors">
    <item><gradient android:endColor="@color/blue" android:startColor="@color/darkblue" /></item>
</integer-array>

它不起作用,我在任何地方搜索,我认为没有人使用它:|你能帮我看看如何在整数数组或 color.xml 文件中使用渐变吗???

【问题讨论】:

    标签: android colors gradient


    【解决方案1】:

    试试这样, 1. 创建可绘制的颜色,例如:

     card_drawable_bg.xml
    
        <?xml version="1.0" encoding="UTF-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape>
                <gradient
    android:startColor="#ff9a6922"
    android:centerColor="#ff9a4025"
    android:endColor="#ff9a0e32"
    android:angle="45"/>
            </shape>
        </item>
    </selector>
    
    1. TypedArray 是您正在寻找的。我有使用它的样本。如果您有兴趣,请查看以下代码:

    首先,res/values/arrays.xml中的整数数组:

        <integer-array name="frag_home_ids">
        <item>@drawable/card_drawable_bg</item>
    
    </integer-array>
    

    其次,以编程方式获取资源整数值:

        TypedArray tArray = getResources().obtainTypedArray(
                R.array.frag_home_ids);
    int count = tArray.length();
    int[] ids = new int[count];
    for (int i = 0; i < ids.length; i++) {
        ids[i] = tArray.getResourceId(i, 0);
    }
    //Recycles the TypedArray, to be re-used by a later caller. 
    //After calling this function you must not ever touch the typed array again.
    tArray.recycle();
    

    第三,像这样调用整数值:

    holder.cardView.setCardBackgroundColor(ids[position]);

    您可以通过这种方式获取字符串、颜色、整数、布局、菜单...的整数值。希望对您有所帮助。

    【讨论】:

    • 创建另一个drawable并添加到整数数组中
    • 阅读此链接以了解有关渐变色的更多信息singhajit.com/gradient-color-in-android
    • @drawable/g1@drawable/g2@drawable/g3 我做到了,但显示白色背景(空 bg)
    • 我认为它是关于 cardView.setCardBackgroundColor 我正在使用渐变
    • 你好?你有解决方案吗?
    【解决方案2】:

    创建一个 xml 文件并将其放在 Drawable 文件夹中。使用以下代码

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <gradient
        android:startColor="#8300bf"
        android:centerColor="#768087"
        android:endColor="#FF9000"
        android:angle="-90"
        android:type="linear"/>
    

    指定 startColor 和 endColor。在您的 LinearLayout 中,通过setBackground="@drawable/Your Gradient Filename" 设置背景。

    【讨论】:

    • 我想要很多渐变色
    • 使用 xml 您最多可以使用 3 种颜色(检查编辑的答案)。如果你想使用更多,你必须使用android.graphics.drawable.GradientDrawable
    • 您希望颜色在固定时间段后保持变化吗?
    • i.stack.imgur.com/L99TH.jpg 看看这张图片。我正在创建它。我做到了,但背景有问题。我的页面有纯色背景,我想要渐变背景...
    • 请附上您的 LinearLayout。
    猜你喜欢
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 2013-04-27
    相关资源
    最近更新 更多