简体   繁体   English

Android:LinearLayout的高度未填充ListView项的父级

[英]Android: height of LinearLayout didn't fill parent of ListView item

I have a ListView, the item is a RelativeLayout, at left is text, and the right side is ImageView in a LinearLayout. 我有一个ListView,该项是RelativeLayout,左边是文本,右边是LinearLayout中的ImageView。
I expect the height of the LinearLayout**(white background)** to be fillParent but it didn't. 我期望LinearLayout **(白色背景)**的高度为fillParent,但事实并非如此。
The effect is : 效果是:
在此处输入图片说明
Anyone can help on this? 有人可以帮忙吗?

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/main_list_item_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/main_list_item_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/main_list_item_more"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/main_list_item_delete_layout"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="#ffffff"
        android:gravity="center"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/main_list_item_delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/main_list_item_check_margin_lr"
            android:layout_marginRight="@dimen/main_list_item_check_margin_lr"
            android:contentDescription="@string/none"
            android:gravity="center"
            android:src="@drawable/ic_ctx_del1" />
    </LinearLayout>

</RelativeLayout>




The main.xml is as below which contains the ListView: main.xml如下所示,其中包含ListView:

    <?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="fill_parent"
    android:gravity="fill"
    android:orientation="vertical" >

    <!-- content -->
    <LinearLayout
        android:id="@+id/center"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="@dimen/main_margin"
        android:layout_marginRight="@dimen/main_margin"
        android:orientation="vertical"
        android:layout_above="@id/adview_ayout">

        <ListView
            android:id="@+id/main_list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </ListView>

    </LinearLayout>
    <!-- content end -->

    <LinearLayout
        android:id="@+id/main_layout_pb"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/transparent"
        android:gravity="center"
        android:orientation="vertical"
        android:clickable="true"
        android:visibility="gone">

        <ProgressBar
            android:id="@+id/main_pb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal" >
        </ProgressBar>
    </LinearLayout>

enter code here

I believe this is something that the parent RelativeLayout can't handle by itself - it creates kind of a vicious circle - parent defers the height to the child ( wrap_content on the RelativeLayout ) and the child defers the height to the parent ( match_parent on main_list_item_delete_layout). 我相信这是父级RelativeLayout本身无法处理的事情-它会创建一个恶性循环-父级将高度推迟到子级( RelativeLayout上的wrap_content ),子级将高度推迟到父级(main_list_item_delete_layout上的match_parent )。 What you can do is to anchor the top of the main_list_item_delete_layout to the parent and its bottom to another view that is guaranteed to be the bottom (the LinearLayout on the left, containing the three text views): 您可以做的是将main_list_item_delete_layout的顶部锚定到父级,将其底部锚定到另一个保证底部的视图(左侧的LinearLayout ,包含三个文本视图):

<RelativeLayout ...>
    <LinearLayout 
        android:id="@+id/left_layout" 
        android:layout_toLeftOf="@+id/main_list_item_delete_layout"
        android:layout_toStartOf="@+id/main_list_item_delete_layout"
        ...
     >
     </LinearLayout>

    <LinearLayout
        android:id="@+id/main_list_item_delete_layout"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_alignBottom="@id/left_layout"
        ...>
    </LinearLayout>
</RelativeLayout>

PS: in your example, in case the text on the left LinearLayout is too long, it will overflow and overlap the delete layout - hence the addition of the anchor I added to the left layout. PS:在您的示例中,如果左侧LinearLayout上的文本太长,它将溢出并与删除布局重叠-因此在左侧布局中添加了锚点。 PS2: I know this solution is very customized to your layout. PS2:我知道此解决方案非常适合您的布局。 If you are set on using the RelativeLayout as a parent, you might want to try the new ConstraintLayout . 如果您将RelativeLayout用作父项,则可能需要尝试新的ConstraintLayout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM