【问题标题】:Android (Xamarin) linearlayout layout:weight not working as intendedAndroid(Xamarin)线性布局布局:重量未按预期工作
【发布时间】:2017-11-14 22:44:10
【问题描述】:

我正在尝试使用 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。为什么它不能垂直工作,我该如何解决这个问题?任何帮助将不胜感激!

【问题讨论】:

  • 将父布局方向设置为垂直
  • @Anmol Strange,我可以发誓我试过了,但它没有用......不过还是谢谢你!另外,您介意回答问题而不是回复,以便我可以将此帖子标记为已回答吗?
  • 当然欢迎

标签: android xamarin layout


【解决方案1】:

将父布局方向设置为垂直

<?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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多