3

我有一个View我试图从中派生的自定义聚合RelativeLayout

public class CheckableView extends RelativeLayout implements Checkable {

TextView        mTextView;
boolean         mIsChecked = false;
int             mId = 0;

public CheckableView(
        Context context, 
        AttributeSet attrs, 
        int defStyle,
        int resource, 
        int textViewResourceId) throws Exception {

    super(context, attrs, defStyle);
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (inflater != null) {
        inflater.inflate(resource, this);
        this.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        mTextView = (TextView) findViewById(textViewResourceId);
        if (mTextView == null) {
            throw new Exception("The specified TextView resource Id was not found.");
        }
    }
}
}

resource参数是我希望聚合的id布局的 id,是我要以图形方式指示状态的 id(通过在其上绘制复选标记)CheckableViewtextViewResourceIdTextViewchecked

这个xml定义为:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content">

<TextView
    android:id="@+id/itemslist_categoryName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:text="@string/app_name" />

<LinearLayout
    android:id="@+id/category_details_price"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/addedit_category_check"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/addedit_category_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="right|center_vertical"
        android:text="@string/app_name" />

    <TextView
        android:id="@+id/addedit_category_price_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="right|center_vertical"
        android:text="@string/app_name" />

</LinearLayout>

</RelativeLayout>

我想将此自定义视图用作适配器ListView中其他特定位置的每一行,我执行以下操作:ListViewgetView

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

        CheckableView view = null;

        if (convertView == null) {
            try {
                view = new CheckableView(getContext(), 
                                                   null, 
                                                   0, 
                                                   R.layout.addedit_group_budget, 
                                                   R.id.addedit_category_check);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            view = (CheckableView) convertView;
        }

        ...
        return view;
    }

的每一行ListView都有不同长度的文本。首次创建视图时,一切似乎都很好,但是当视图开始被回收时(即当我向上/向下滚动时)。TextViews即使我已经wrap_content指定了宽度,它似乎也没有调整大小以适应文本的大小。

我会错过什么?下面的示例屏幕截图。请注意,有些文本是椭圆形的,即使显然还有更多文本的空间。

文本省略号,即使有更多空间

如果我从类定义中删除implements Checkable,事情会按预期工作,表明这与它有关。

4

1 回答 1

2

发现真正的罪魁祸首是setCompoundDrawablesWithIntrinsicBounds我在其中一个 TextView 上制作的 API。调用这个 API 似乎会破坏父布局中 View 的测量值。这个问题很好地表达了@ 在具有相同背景的多个视图上调用 setCompoundDrawablesWithIntrinsicBounds 给出不一致的大小

我能够通过使用 ImageView 来解决该问题,在该 ImageView 中我设置了适当的图标以指示“已检查”状态。

于 2012-10-30T18:56:40.173 回答