我有以下形状可绘制:
<?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"
/>
我怎么能以编程方式对形状做同样的事情,以便我可以改变它的颜色?
答案 0 :(得分:0)
您可以使用ShapeDrawable
访问代码中的形状,然后将getView方法中的Shape颜色更改为:
@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;
}
P.S。 - 这是一个伪代码
答案 1 :(得分:0)
@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;
}