Android 如何根据内容更改listView行高?

Android 如何根据内容更改listView行高?,android,android-layout,android-listview,textview,Android,Android Layout,Android Listview,Textview,我有下面的布局,它对应于列表视图中的一行。我希望中间的TeXVIEW根据文本的长度展开。 在下面的布局中,我的文本视图甚至不可见。有人能指出我犯了什么错误吗 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:

我有下面的布局,它对应于列表视图中的一行。我希望中间的TeXVIEW根据文本的长度展开。 在下面的布局中,我的文本视图甚至不可见。有人能指出我犯了什么错误吗

<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>
试试这个

从内容文本视图中删除android:layout_=@+id/actions 将下面的android:layout_=@+id/内容添加到您的操作线性布局中 从操作中删除android:layout\u alignParentBottom=True
如果它仍然不使用LinearLayout而不是RelativeLayout,那么它可能会工作。

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

你也能在ListView适配器中发布代码吗?我观察到,当我为ListView行设置固定高度时,feedContent字段是可见的。工作起来很有魅力。你能解释一下为什么订购对RelativeLayout很重要吗?我以前也遇到过这样的订购问题,但没有记录在案。我真的不喜欢realtive布局,所以我只在必要时才使用它,这是非常罕见的。问题是您有3个视图-第一个视图有alignParentTop,第二个视图在第一个视图的下方但在第三个视图的上方,第三个视图有alignParentBottom。结果是相对布局实际上跳过了第二个子元素的换行内容,它的工作方式与match_parent类似。所有内容都没有包装,而是尽可能高地展开,这对于listView来说是非常错误的。
 @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;
    }