如何改变Android的绘制形状颜色 [英] How to change color of drawable shapes in android

查看:223
本文介绍了如何改变Android的绘制形状颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发小型的Andr​​oid应用程序中,我设定绘制资源为背景的线性布局。现在我想做的线性布局更改背景颜色动态,但绘制资源之内。
我的code如下:

I am developing small android application in which I set drawable resource as background for linear layout. Now what I want to do change background color of linear layout dynamically, but within drawable resource. My code looks like :

//  bcd.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item>        
    <shape>
        <gradient
            android:endColor="#22000000"
            android:startColor="#22000000"
            android:angle="270" />
        <stroke
            android:width="3dp"
            android:color="@color/white" />
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>

<LinearLayout 
android:id="@+id/lin_llt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>

和我这样的我的活动设置为线性布局的背景...

and I set background for linear layout in my activity like this...

parentLayout = (LinearLayout) view.findViewById(R.id.lin_llt);
parentLayout.setBackgroundResource(R.drawable.bcd);

现在我想做的事情,我想改变我的意思改变我带圆角和填充线性布局的颜色定义绘制绘制资源的颜色。

Now what I want to do i want to change color of my drawable resource that mean change color of my linear layout with rounded corner and padding define in drawable..

我下面的方式尝试过这种

I tried this in following way

ShapeDrawable bgShape = (ShapeDrawable )parentLayout.getBackground();
bgShape.getPaint().setColor(Color.BLACK);

但它不是为我工作。任何其他解决方案。

but its not working for me. any other solution .

那么怎么办呢?
需要帮助...
谢谢...

So how to do it... Need help... thank you...

推荐答案

动态改变布局的颜色

LinearLayout Layout = (LinearLayout) findViewById(R.layout.id);
Layout.setBackgroundColor(Color.parseColor("#ffffff"));

动态设置背景颜色渐变

Dynamically set the background color gradient

View layout = findViewById(R.id.mainlayout);

GradientDrawable gd = new GradientDrawable(
        GradientDrawable.Orientation.TOP_BOTTOM,
        new int[] {0xFF616261,0xFF131313});
gd.setCornerRadius(0f);

layout.setBackgroundDrawable(gd);

这篇关于如何改变Android的绘制形状颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆