【问题标题】:Create Drawable with borders completely programmatically完全以编程方式创建带边框的 Drawable
【发布时间】:2013-02-15 12:37:52
【问题描述】:

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

【问题讨论】:

  • 找到了答案。只需使用 LayerDrawable 的 setLayerInset。见stackoverflow.com/questions/12427025/…
  • 你应该把你找到的答案写成你问题的真实答案,然后接受它作为答案。

标签: android border layer


【解决方案1】:
ImageView imageView1 = (ImageView) findViewById(R.id.imgView1);
    ((GradientDrawable ) imageView1.getBackground()).setColorFilter(
            Color.RED,  PorterDuff.Mode.SRC_ATOP);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多