【问题标题】:Altered GradientDrawable gets unwantedly reused in different places改变的 GradientDrawable 在不同的地方被不必要地重复使用
【发布时间】:2015-12-16 00:25:02
【问题描述】:

我有一个 ShapeDrawable drawable/my_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">
    <solid
       android:color="#FFFF00" />
    <padding android:left="1dp"
        android:top="1dp"
        android:right="1dp"
        android:bottom="1dp" />
</shape>

我在我的应用程序的几个地方将其用作一些TextViews 的背景。就像 layout/my_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout...>
    <TextView
        android:id="@+id/tag"
        android:background="@drawable/my_drawable"
        android:layout_height="wrap_content"
        android:layout_width="wrap"
        android:text="Some text"
    />
<!-- other tags -->
</RelativeLayout>

或在用作列表项的布局中layout/my_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout...>
    <TextView
        android:id="@+id/subject"
        android:background="@drawable/my_drawable"
        android:layout_height="wrap_content"
        android:layout_width="wrap"
    />
<!-- other tags -->
</RelativeLayout>

现在,当我在列表的ArrayAdapter 内以编程方式更改形状的颜色时,TextView我的应用程序中完全不同的位置 的片段内的背景会得到与我列表中的最后一项颜色相同!从那时起,颜色改变的形状被用于布局中包含 my_fragment.xml 的所有其他活动。

holder.subject = (TextView) v.findViewById(R.id.subject);

GradientDrawable bgShape = (GradientDrawable)holder.subject.getBackground();
bgShape.setColor( position % 2 == 0 ? Color.BLACK : Color.WHITE );

这怎么可能?如何避免这种“重用”行为?

这是我的数组适配器的代码:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    ViewHolder holder; // to reference the child views for later actions

    if (v == null) {
        LayoutInflater vi = 
            (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.my_list_item, null);
        // cache view fields into the holder
        holder = new ViewHolder();
        holder.subject = (TextView) v.findViewById(R.id.subject);
        // associate the holder with the view for later lookup
        v.setTag(holder);
    }
    else {
        // view already exists, get the holder instance from the view
        holder = (ViewHolder) v.getTag();
    }

    GradientDrawable bgShape = (GradientDrawable)holder.subject.getBackground();
    bgShape.setColor( position % 2 == 0 ? Color.BLACK : Color.WHITE );
    holder.subject.setText(getItem(position));
}

// somewhere else in your class definition
static class ViewHolder {
    TextView subject;
}

【问题讨论】:

标签: android layout drawable shapedrawable


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 2023-03-12
  • 2012-06-26
  • 2017-11-17
  • 2020-05-11
  • 2015-12-02
  • 2015-11-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多