当前位置: 动力学知识库 > 问答 > 编程问答 >

Android Drawable question

问题描述:

I am trying to create a drawable in code and change the color based on some criteria. I can get it to work but it doesn't want to let me set the padding on the view. Any help would be appreciated.

<?xml version="1.0" encoding="utf-8"?>

<ImageView

android:id="@+id/icon"

android:layout_width="50px"

android:layout_height="fill_parent"

/>

<TextView

android:id="@+id/label"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:paddingLeft="17px"

android:textSize="28sp"

/>

 ImageView icon = (ImageView) row.findViewById(R.id.icon);

ShapeDrawable mDrawable;

int x = 0;

int y = 0;

int width = 50;

int height = 50;

float[] outerR = new float[] { 12, 12, 12, 12, 12, 12, 12, 12 };

mDrawable = new ShapeDrawable(new RoundRectShape(outerR, null, null));

mDrawable.setBounds(x, y+height, x + width, y);

switch(position){

case 0:

mDrawable.getPaint().setColor(0xffff0000); //Red

break;

case 1:

mDrawable.getPaint().setColor(0xffff0000); //Red

break;

case 2:

mDrawable.getPaint().setColor(0xff00c000); //Green

break;

case 3:

mDrawable.getPaint().setColor(0xff00c000); //Green

break;

case 4:

mDrawable.getPaint().setColor(0xff0000ff); //Blue

break;

case 5:

mDrawable.getPaint().setColor(0xff0000ff); //Blue

break;

case 6:

mDrawable.getPaint().setColor(0xff696969); //Gray

break;

case 7:

mDrawable.getPaint().setColor(0xff696969); //Gray

break;

case 8:

mDrawable.getPaint().setColor(0xffffff00); //Yellow

break;

case 9:

mDrawable.getPaint().setColor(0xff8b4513); //Brown

break;

case 10:

mDrawable.getPaint().setColor(0xff8b4513); //Brown

break;

case 11:

mDrawable.getPaint().setColor(0xff8b4513); //Brown

break;

case 12:

mDrawable.getPaint().setColor(0xffa020f0); //Purple

break;

case 13:

mDrawable.getPaint().setColor(0xffff0000); //Red

break;

case 14:

mDrawable.getPaint().setColor(0xffffd700); //Gold

break;

case 15:

mDrawable.getPaint().setColor(0xffff6600); //Orange

break;

}

icon.setBackgroundDrawable(mDrawable);

icon.setPadding(5, 5, 5, 5);

If I set the padding in XML it just ignores it.

Thanks,

Rob

网友答案:

Try setting padding for the ShapeDrawable(link).

And one more thing is, padding will move the image view contents not the background ( i.e image view itself). You are setting the drawable as ImageView background.

Use setImageDrawable() function to get the image view setPadding() effect.

分享给朋友:
您可能感兴趣的文章:
随机阅读: