首页 文章

Android(Xamarin)linearlayout布局:重量不按预期工作

提问于
浏览
1

我正在尝试使用layout_weight实现一个垂直分隔屏幕40/60的布局 .

这是xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:background="@drawable/background">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.4">
        <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@color/white" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.6">
        <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@color/black" />
    </LinearLayout>
</LinearLayout>

此代码不起作用,返回空白屏幕 . 但是,如果我改变了

android:layout_width =“fill_parent”android:layout_height =“0dp”android:layout_weight =“0.4”

(垂直分割)

android:layout_width =“0dp”android:layout_height =“fill_parent”android:layout_weight =“0.4”

(水平分割)

代码按预期工作,布局水平分割40/60 . 为什么它不能垂直工作,我该如何解决这个问题呢?任何帮助,将不胜感激!

1 回答

  • 2

    将父布局方向设为垂直方向

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1"
    android:focusable="true"
    android:orientation="verticle"
    android:focusableInTouchMode="true"
    android:background="@drawable/background">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.4">
        <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@color/white" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.6">
        <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@color/black" />
    </LinearLayout>
    

相关问题