gpt4 book ai didi

android - LinearLayout 高度随添加新 View 而变化

转载 作者:行者123 更新时间:2023-11-29 00:21:18 27 4
gpt4 key购买 nike

我的 GUI 中有一些嵌套布局。不要问我为什么要那样做或者为什么不使用网格布局...我的问题是别的。

我的布局看起来像 THIS

在我向这 4 个相关布局中的任何一个添加任何 View (比方说一个按钮)后,它变为 THIS

我将相对布局的权重设置为 0dp。这样,当我向布局添加另一个 View 时,宽度不会改变。但我不知道如何处理高度。如果我也将它设置为 0dp,它就会消失。

这是我的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/rising_sun_blue"
android:weightSum="2">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:background="#333111"
android:weightSum="2"
android:layout_alignParentBottom="true"
>

<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff">
</RelativeLayout>

<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff0000" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="Button" />

</RelativeLayout>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:background="#123132"
android:weightSum="2"
android:layout_alignParentTop="true">

<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff0000">
</RelativeLayout>

<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff">
</RelativeLayout>


</LinearLayout>
</LinearLayout>

所以,我的问题是,在我向其中添加一堆新 View 后,是否有任何代码可以“粘附”我的布局并使它们与第一个屏幕截图完全一样!?

最佳答案

给两个嵌套的 LinearLayouts android:layout_height="match_parent"而不是 wrap_content。

关于android - LinearLayout 高度随添加新 View 而变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22956932/

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