gpt4 book ai didi

android - android 的 LinearLayout 的 layout_weight 不起作用

转载 作者:太空狗 更新时间:2023-10-29 15:36:32 25 4
gpt4 key购买 nike

我是 android 的新手,我正在使用线性布局。我想放置一张占屏幕 20% 的图像。这是代码,但这不起作用。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/sky" >

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/schoolroad" />

</RelativeLayout>

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="8" >
</RelativeLayout>

</LinearLayout>

结果是左侧布局占了屏幕的 80%。

我的代码有什么问题?

最佳答案

你应该指定你的 LinearLayout 的方向

并且当您使用权重时,您应该将宽度(或高度)设置为与您的方向相对应的 0dp。

因此,如果您的方向是垂直的,则在使用重量时应将高度设置为零。如果它是水平的,那么在使用权重时宽度应该为零。

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/sky"

android:orientation="vertical"

>

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="2" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/schoolroad" />
</RelativeLayout>

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="8" >
</RelativeLayout>

</LinearLayout>

关于android - android 的 LinearLayout 的 layout_weight 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17240266/

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