【问题标题】:Android height wrap_content with fix ratioAndroid 高度 wrap_content 与固定比率
【发布时间】:2019-01-23 08:17:47
【问题描述】:

我的XML 文件有一个LinearLayout,其中包含两个ListViewandroid:height="wrap_content"。但是,当其中一个ListView 的内容过多时,它会推送另一个ListView 并占用它的空间。我该怎么做才能让它在一定高度停止推动?我尝试添加layout-weightmaxHeight,但没有帮助。

【问题讨论】:

  • 如果要使用layout-weight,请尝试将高度设置为match_parent或0dp
  • @PriyankVadariya 有趣...match_parent 和 0dp 有什么区别?笔记本电脑不在我身边,所以我无法对此进行测试。如果你能解释一下就太好了。

标签: android listview height android-linearlayout android-wrap-content


【解决方案1】:

如果您希望两个 ListViews 均分相同的金额,只需将它们放入 LinearLayout

LinearLayout 将拥有weightSum=2 属性,而ListViews 将拥有,具体取决于LinearLayout orientation property

如果 vertical -> ListViews 将有 android:height="0dp"layout-weight=1

如果 horizontal -> ListViews 将有 android:width="0dp"layout-weight=1

【讨论】:

  • 嗨,我希望他们随意扩展,但在某些时候停止扩展。 android:height="0dp" 将根据体重固定它们的高度。
【解决方案2】:

我通过编程解决了这个问题。似乎XML 没有对动态高度进行太多控制。我在ListView 中添加了onLayoutChangedListener,以检测高度变化并在侦听器中添加高度控制逻辑。

【讨论】:

    【解决方案3】:

    试试这个:

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/green_bg_duplicate"
        android:gravity="center"
        android:orientation="vertical"
        android:weightSum="2">
    
        <RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="1" />
    
        <RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="1" />
    
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      • 2018-11-28
      相关资源
      最近更新 更多