gpt4 book ai didi

android - 重量和 wrap_content 的布局高度问题

转载 作者:行者123 更新时间:2023-11-30 01:11:48 32 4
gpt4 key购买 nike

我的观点是这样的:

<LinearLayout 
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
/>
</LinearLayout>

它将为 TextView 提供 50dp,并将其余区域提供给 RecyclerView。但是,如果 RecyclerView 中的项目小于,比如说 1 或 2,那么它应该缩小到它的 subview 的高度,否则就像布局指定的那样。谁能帮忙??

最佳答案

我认为最好的选择是动态更改 Root View 的 LinearLayout.LayoutParams。当 RecyclerView 的子级数量较少时,将 LinearLayout 参数设置为:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
layout.setParams(params);

否则,保持原样。

很容易检测 RecyclerView 是否应该被包装。获取LinearLayout高度:

int height = layout.getTop() - layout.getBottom()

如果此高度低于预期的最大高度(屏幕高度或其他预定义值)。

关于android - 重量和 wrap_content 的布局高度问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38359938/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com