【问题标题】:Android how to set height of linear layout as listview elements?Android如何将线性布局的高度设置为listview元素?
【发布时间】:2018-05-18 06:42:25
【问题描述】:

Android如何将线性布局的高度设置为listview元素? 我正在使用带有线性布局的列表视图,所以我可以做到这一点。请帮我。 我该如何解决? Android如何将线性布局的高度设置为listview元素? 我正在使用带有线性布局的列表视图,所以我可以做到这一点。请帮我。 我该如何解决?

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:id="@+id/relative"
            android:orientation="vertical"
            android:background="@drawable/shap">

            <ProgressBar
                android:id="@+id/progressBar"
                android:visibility="gone"
                style="?android:attr/progressBarStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentStart="true"
                android:layout_marginBottom="51dp"
                android:layout_marginStart="147dp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:orientation="vertical"
                android:id="@+id/lout"
                android:layout_marginBottom="10dp"
                android:background="@android:color/holo_red_light"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="More Related Products"
                    android:textSize="20dp"
                    android:padding="5dp"
                    android:textColor="#ffffff"
                    android:textAlignment="center"/>
            </LinearLayout>
            <ListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/product_list"
                android:dividerHeight="1dp"
                android:focusable="true"
                android:layout_below="@+id/lout">
            </ListView>

        </LinearLayout>

【问题讨论】:

  • 请分享您要实现的设计
  • 你说的是哪种线性布局?
  • 检查相对id线性布局@rya
  • @hasan_shaikh 我想要将高度自动设置为列表视图元素的线性布局
  • 非常混乱。哪个LinearLayout?如果您设置第二个LinearLayout 高度,则ListView 将是屏幕外的GONE,因为父级也是LinearLayout。请解释问题或显示预期输出。而外层LinearLayout会自动调整高度,因为它的wrap_content

标签: android android-layout listview android-linearlayout


【解决方案1】:

你可以这样做,

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/product_list"
        android:dividerHeight="1dp"
        android:focusable="true"
        android:layout_below="@+id/lout">
    </ListView>
</LinearLayout>

这会将您的线性布局包装到列表视图中

【讨论】:

    【解决方案2】:

    将 listView 高度设置为 wrap_content 是不好的做法,因为它总是会调用 notifydatasetchanged()。您必须使其成为 match_parent。然后你的布局高度将与 listView 相同。

    你可以试试这样的:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/product_list"
            android:dividerHeight="1dp"
            android:focusable="true"
            android:layout_below="@+id/lout">
        </ListView>
    </LinearLayout>
    

    或者类似的东西:

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
    
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            //TO DO: YOUR CONTENT HERE
    
        </LinearLayout>
    <ListView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/product_list"
                android:dividerHeight="1dp"
                android:focusable="true"
                android:layout_below="@+id/lout">
            </ListView>
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 2020-11-23
      • 1970-01-01
      • 2018-04-28
      • 2023-03-04
      • 1970-01-01
      • 2021-05-03
      • 2012-01-11
      • 2021-03-02
      • 1970-01-01
      相关资源
      最近更新 更多