【问题标题】:How to make the listView row height change based on content?如何根据内容改变listView行高?
【发布时间】:2015-02-19 19:18:09
【问题描述】:

我有以下布局,它对应于列表视图中的一行。我希望中间的 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;
    }

【问题讨论】:

  • 你能把代码也贴在你的listview适配器中吗?
  • 我观察到,当我为 listView 行设置固定高度时,feedContent 字段是可见的。

标签: android android-layout android-listview textview


【解决方案1】:

试试这个...

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

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

【讨论】:

  • 工作就像一个魅力。你能解释一下为什么排序对于RelativeLayout很重要吗?我以前遇到过这样的订购问题,但没有记录在案。
  • 我不太喜欢真实布局,所以我只在必要时使用它(这非常罕见)。问题是您有 3 个视图 - 第一个具有 alignParentTop,第二个具有“低于第一个但高于第三个”,第三个具有“alignParentBottom”。结果是相对布局实际上跳过了第二个孩子的包装内容,它像“match_parent”一样工作。一切都没有被包裹,而是尽可能地展开,这对于 listView 来说是非常错误的。
【解决方案2】:

在我的 ListView 中,每一行的高度都会根据数据自动变化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-20
    • 2019-06-06
    • 1970-01-01
    • 2022-06-23
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 2020-03-18
    相关资源
    最近更新 更多