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 的等价物。有人可以帮助我吗?

4

1 回答 1

0
ImageView imageView1 = (ImageView) findViewById(R.id.imgView1);
    ((GradientDrawable ) imageView1.getBackground()).setColorFilter(
            Color.RED,  PorterDuff.Mode.SRC_ATOP);
于 2013-12-05T02:55:03.977 回答