【问题标题】:Change color of shape [duplicate]改变形状的颜色[重复]
【发布时间】:2015-08-19 13:17:07
【问题描述】:

我有以下可绘制的形状:

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

在我的主布局中它是这样调用的:

<ImageView
    android:id="@+id/circle_bg"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentLeft="true"
    android:background="@drawable/circle_bg" />

我有一个适配器,用于用其他数据填充布局,但其中一个数据是十六进制颜色代码。这是我如何填充布局的主要内容的示例:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Post post = getItem(position);

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.post_layout, parent, false);
    }

    TextView content = (TextView) convertView.findViewById(R.id.content);
    content.setText(article.getContent());

    return convertView;
}

填充这部分布局:

<TextView
    android:id="@+id/content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    />

如何以编程方式对形状执行相同操作,以便更改其颜色?

【问题讨论】:

  • @SilentKiller 我不处理渐变。
  • 关于同一个问题还有其他答案描述了如何使用ShapeDrawable 更改颜色也检查了?
  • @bpA 同样,我没有使用渐变。所有这些答案都与渐变形状有关。
  • 你为什么在 ONE 层中使用layer-list?你稍后会打电话给addLayer/setDrawable吗?

标签: android android-layout android-activity android-xml


【解决方案1】:

您可以使用ShapeDrawable 访问代码中的形状,然后在 getView 方法中将形状的颜色更改为:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

Post post = getItem(position);

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.post_layout, parent, false);
    }

    ImageView content = (ImageView)convertView.findViewById(R.id.content);
    ShapeDrawable ovalShape = (ShapeDrawable)content.getBackGround();
    ovalShape.getPaint().setColor(Color.parseColor("hexcolor"));
    return convertView;
}

附: - 这是一个伪代码

【讨论】:

  • 我明白我会如何用伪代码来做这件事,这就是为什么这不是真的有用。
【解决方案2】:
@Override
public View getView(int position, View convertView, ViewGroup parent) {

Post post = getItem(position);

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.post_layout, parent, false);
    }

    ImageView content = (ImageView)convertView.findViewById(R.id.content);
    LayerDrawable ovalShape = (LayerDrawable)content.getBackGround();
    ovalShape.setColorFilter(Color.parseColor("hexcolor"), PorterDuff.Mode.SRC);
    return convertView;
}

【讨论】:

  • 不填充整个背景,是否可以只填充形状?我计划稍后在形状中添加一个图像,您的方法意味着即使图像也会被颜色重叠。
  • 您是否尝试过您先尝试的要求,如果不起作用,请给我您的代码,我会检查
猜你喜欢
  • 2014-08-11
  • 1970-01-01
  • 2017-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-02
  • 1970-01-01
相关资源
最近更新 更多