Android自定义列表项

时间:2014-09-22 19:13:53

标签: android listview

我使用以下XML作为列表项布局:

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



    <RelativeLayout
        android:id="@+id/currentUserIndicator"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@color/blue" />

    <TextView
        android:id="@+id/workerName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/currentUserIndicator"
        android:padding="30dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

它看起来像那样:

enter image description here

现在的问题是,当我在列表适配器上使用它时:

    adapter = new ArrayAdapter<WorkerUser>(this, R.layout.list_item_worker, workerList) {
        @Override
        public View getView(final int position, final View convertView, final ViewGroup parent) {
            final View container;
            if (convertView == null) {
                container = inflater.inflate(R.layout.list_item_worker, parent, false);
            } else {
                container = convertView;
            }
            final TextView view = (TextView) container.findViewById(R.id.workerName);
            final View indicator = container.findViewById(R.id.currentUserIndicator);
            final WorkerUser item = getItem(position);
            view.setText(item.firstname + " " + item.lastname);
            return container;
        }
    };

它只是不起作用,左边的蓝色矩形不会显示!有谁知道为什么?

1 个答案:

答案 0 :(得分:2)

您已关闭其中包含0个孩子的RelativeLayout。因为它有0个孩子,所以它的高度设置为zero,因此它已经消失了。您需要在其中放置一些元素才能显示或设置dp中的高度 我猜你试图将TextView放在RelativeLayout内,如果是这样的话就试试吧

<RelativeLayout
android:id="@+id/currentUserIndicator"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@color/blue" >

<TextView
android:id="@+id/workerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/currentUserIndicator"
android:padding="30dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>