繁体   English   中英

Android Layout_weight

[英]Android Layout_weight

也许我误解了layout_weight参数,但我无法理解为什么这样的东西不起作用......

<?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="horizontal" android:weightSum="1">

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="TextView" android:layout_weight="0.3" android:background="@color/apr_blue"/>

<TextView
    android:id="@+id/textView2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.2"
    android:text="TextView" 
    android:background="@color/apr_brightyellow"/>

<TextView
    android:id="@+id/textView3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:text="TextView" 
    android:background="@color/apr_yellow"/>

</LinearLayout>

我在LinearLayout中有三个或更多文本视图。 我想将它们放在宽度为百分比的列中。 这就是它产生的东西:

layout_weight设置为0.3 / 0.2 / 0.5

Android layout_weight不正确

然后我改变了layout_weight参数,如下所示:

<?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="horizontal" android:weightSum="1">

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="TextView" android:layout_weight="0.3" android:background="@color/apr_blue"/>

<TextView
    android:id="@+id/textView2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.3"
    android:text="TextView" 
    android:background="@color/apr_brightyellow"/>

<TextView
    android:id="@+id/textView3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.4"
    android:text="TextView" 
    android:background="@color/apr_yellow"/>

</LinearLayout>

现在我按照自己的意愿得到它:

layout_weight设置为0.3 / 0.2 / 0.5

Android layout_weight正确

它是一个错误还是我真的误解了layout_weight的全部内容?

当你使用android:layout_weight你应该设置android:layout_width="0dp" (如果是垂直方向 - android:layout_height="0dp" )。

在此输入图像描述

尝试这个....

        <TextView
            android:background="#F2e"
            android:layout_weight="3"
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
             android:background="#F6e"
            android:layout_weight="2"
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
             android:background="#F45"
            android:layout_weight="5"
            android:id="@+id/textView3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="TextView" />

    </LinearLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM