Why do ListView items not grow to wrap their content?

前端 未结 9 644
猫巷女王i
猫巷女王i 2020-11-30 20:26

I have a rather complex ListView, with variable list item heights. Under certain conditions, I need to display an additional view in a list item, which is hidden by default

相关标签:
9条回答
  • 2020-11-30 20:44

    Things to note about column alignment:

    • The container LinearLayout element uses horizontal orientation and android:layout_width as match_parent.
    • The TextView elements use android:layout_width="0dp"
    • Each of the two TextView elements uses android:layout_weight="1" so that they use equal amounts of the available width.
    • The android:layout_width property of each TextView element is set to "0dp".
    0 讨论(0)
  • 2020-11-30 20:45

    The problems with layout could be caused by ScrollView to be the wrapper

    I stumbled upon some note in http://developer.android.com/reference/android/widget/ExpandableListView.html

    "...Note: You cannot use the value wrap_content for the android:layout_height attribute of a ExpandableListView in XML if the parent's size is also not strictly specified (for example, if the parent were ScrollView you could not specify wrap_content since it also can be any length. However, you can use wrap_content if the ExpandableListView parent has a specific size, such as 100 pixels."

    I removed wrapping ScrollView and linear layout started working properly. Now its only to understand how to wrap the stuff to ScrollView. God help me

    But anyway this is really weird behavior. I think that fill_parent is not really correct wording. When using heirarchyviewer tool I always see WRAP_CONTENT and MATCH_PARENT values for layout_width and leayout_height. So probably fill_parent is actually means match_parent which puts me in cognitive dissonance.

    0 讨论(0)
  • 2020-11-30 20:57

    I managed to fix this, but I don't understand why.

    As I mentioned, I had set the layout_height of the list item layout to wrap_content (since fill_parent is meaningless here, considering that a ListView is indefinitely tall).

    However, I had set the layout_height of all views inside that layout to fill_parent. The problem disappeared when setting them to wrap_content instead.

    This raises two other questions:

    1) What are the semantics of a view asking to fill_parent, when the parent wraps_content? Which size request takes precedence?

    2) How would I ever make a view fill a list item if fill_parent apparently doesn't work?

    Thanks for your input guys.

    0 讨论(0)
  • 2020-11-30 20:59

    I use the "AbsListView.LayoutParams" to configure the width and height manually inside "Adapter.getView()".

    0 讨论(0)
  • 2020-11-30 20:59

    I had the same problem: i would like to have a list inside my activity that fill all the screen either when the device is in vertical than in horizontal orientation. I solve the problem using al Linear Layout that have height that have to fill_parent, and setting the height of the item in the list to 0dp while the layout_weight is setter to 1.

    android:layout_weight="1"
    android:layout_height="0dp"
    
    0 讨论(0)
  • 2020-11-30 20:59

    this works for me

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    
        android:padding="@dimen/activity_vertical_margin">
    
        <TextView
            android:id="@+id/tv_status"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="17sp"
            android:text="@string/text_list_devices" />
    
        <ListView
            android:id="@+id/lv_paired"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/activity_vertical_margin"
            android:cacheColorHint="#00000000"
            android:layout_above="@+id/signup_t"
            android:layout_below="@id/tv_status"
            />
    
        <Button
    
            android:id="@+id/signup_t"
            style="?android:textAppearanceSmall"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:textColor="@android:color/white"
            android:layout_alignParentBottom="true"
            android:text="Print All Records"
            android:typeface="sans"
            android:layout_marginLeft="45dp"
            android:layout_marginRight="45dp"
            android:textSize="24sp"
            android:background="@drawable/selector_for_button"
            android:textStyle="bold" />
    </RelativeLayout>
    
    0 讨论(0)
提交回复
热议问题