如何根据内容使listView行高改变?

时间:2014-12-21 13:57:07

标签: android android-layout android-listview textview

我有以下布局,对应于列表视图中的一行。我希望中间的TextView根据文本的长度进行扩展。 使用以下布局,我的textview甚至不可见。有人可以指出我在做什么错误吗?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="3dip"
    android:paddingLeft="6dip"
    android:paddingRight="6dip" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_centerVertical="true"
        android:clickable="true"
        android:scaleType="fitXY"
        android:src="@drawable/batman" />

    <LinearLayout
        android:id="@+id/user_info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/imageView1"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
        android:paddingLeft="10dp"
        android:paddingTop="5dp" >

        <TextView
            android:id="@+id/username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/display_name_color"
            android:textSize="14dp" />

        <TextView
            android:id="@+id/handle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="15dp"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textSize="12dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/actions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/imageView1"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
        android:paddingTop="5dp" >

        <ImageView
            android:id="@+id/favourite"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="40dp"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/bookmark"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="60dp"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <TextView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/actions"
        android:layout_below="@+id/user_info"
        android:layout_toRightOf="@+id/imageView1"
        android:paddingBottom="5dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="5dp"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

这是我的适配器的getView函数。

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


        ImageView imageView;
        if (convertView == null) { 

            LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_row, container, false);


            imageView = (ImageView)convertView.findViewById(R.id.imageView1);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

        } else { 
            imageView = (ImageView)convertView.findViewById(R.id.imageView1);
        }

        imageView.setTag(position);
        imageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                int position = Integer.parseInt(v.getTag().toString());
                mOnProfilePicClickCallback.onItemClick(position);
            }
        });
        //Load TextFields here
        Feed feed = (Feed) getItem(position);
        User user = null;
        if (feed != null) {
            user = (User) CommonData.users.get(feed.owner);
            TextView handle = (TextView) convertView.findViewById(R.id.handle);
            TextView userName = (TextView) convertView.findViewById(R.id.username);
            TextView feedContent = (TextView) convertView.findViewById(R.id.feedcontent);

            handle.setText(feed.owner);
            if(tweeter!=null && !TextUtils.isEmpty(user.displayname))
            {
                userName.setText(user.displayname);
            }
            else
            {
                userName.setText(feed.owner);
            }
            feedContent.setText(feed.feedcontent);      
        }


        // Finally load the image asynchronously into the ImageView, this also takes care of
        // setting a placeholder image while the background thread runs
        if(user!=null)
        {
            mImageFetcher.loadImage(user.profileimageurl, imageView);
        }
        return convertView;
    }

2 个答案:

答案 0 :(得分:0)

试试这个......

  • 从“内容”TextView
  • 中删除android:layout_above="@+id/actions"
  • android:layout_below="@+id/content"添加到“动作”LinearLayout
  • 从“操作”中删除android:layout_alignParentBottom="true"

如果它仍然不使用LinearLayouts而不是RelativeLayout,它可能会起作用。

答案 1 :(得分:0)

在我的ListView中,每行高度都会根据数据自动更改。