gpt4 book ai didi

android - 当我将 View 添加到布局中时 layout_weight 发生变化 (Android)

转载 作者:行者123 更新时间:2023-11-30 02:53:45 25 4
gpt4 key购买 nike

我正在 Android 中为此列表项创 build 计:

enter image description here

我认为最好的解决方案是创建一个线性布局,其中包含两个相对布局和一个空间。所以我尝试这样做,但我对重量有疑问。在这张图片中,您可以看到我使用 linear_layout 的 layout_weight 添加了两个带有空格的相对布局,并且比例有效。

enter image description here

这是代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/fondo_chiste_lista_item"
android:weightSum="147"
android:orientation="vertical" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_weight="41"
android:layout_height="0dp" >
</RelativeLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_weight="94"
android:layout_height="0dp" >
</RelativeLayout>

<android.support.v7.widget.Space
android:id="@+id/space1"
android:layout_width="wrap_content"
android:layout_weight="12"
android:layout_height="0dp" />

</LinearLayout>

但是,现在如果我在相对布局比例内添加一个 TextView ,它将不起作用。如您所见,相关布局的框具有不同的大小。

enter image description here

这是代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/fondo_chiste_lista_item"
android:weightSum="147"
android:orientation="vertical" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_weight="41"
android:layout_height="0dp" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="46dp"
android:text="TextView" />

</RelativeLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_weight="94"
android:layout_height="0dp" >
</RelativeLayout>

<android.support.v7.widget.Space
android:id="@+id/space1"
android:layout_width="wrap_content"
android:layout_weight="12"
android:layout_height="0dp" />

</LinearLayout>

那么……问题是什么?为什么添加元素时比例会发生变化?有没有更好的解决方案?

最佳答案

我将添加 is 作为答案,因为它解决了您的问题

代替

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

使用类似的东西

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"

关于android - 当我将 View 添加到布局中时 layout_weight 发生变化 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23684717/

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