2013-02-15 28 views
1

我有以下XML:创建可绘制带边框完全编程

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item> 
    <shape 
     android:shape="rectangle"> 
     <stroke android:width="1dp" android:color="#000000" /> 
     <solid android:color="#000000" /> 
    </shape> 
    </item> 

    <item android:top="0dp" android:bottom="1dp"> 
    <shape 
     android:shape="rectangle"> 
     <stroke android:width="1dp" android:color="#414141" /> 
     <solid android:color="#414141" /> 
    </shape> 
    </item> 

    <item android:top="1dp" android:bottom="1dp"> 
    <shape 
     android:shape="rectangle"> 
     <stroke android:width="1dp" android:color="#2C2C2C" /> 
     <solid android:color="#2C2C2C" /> 
    </shape> 
    </item> 
</layer-list> 

这不正是我想要的,但我不能使用它。我想在运行时更改三层的颜色,所以我必须完全以编程方式完成此操作。我知道我必须使用LayerDrawable,但我不知道如何实现相当于android:top和android:bottom。有人能帮助我吗?

+0

找到了答案。只需使用LayerDrawable的setLayerInset即可。请参阅http://stackoverflow.com/questions/12427025/android-gradient-drawable-programmatically – anel 2013-02-15 13:37:34

+0

您应该将您发现的答案写为对您问题的真实答案,然后将其作为答案接受。 – 2013-11-02 16:07:54

回答

0
ImageView imageView1 = (ImageView) findViewById(R.id.imgView1); 
    ((GradientDrawable) imageView1.getBackground()).setColorFilter(
      Color.RED, PorterDuff.Mode.SRC_ATOP);