为什么layout中的layout_weight的效果与TextView的行为不同?

时间:2017-07-27 04:34:49

标签: android xml layout

我尝试使用TextView:

以1:2:3的比例设置3个彩色区域
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TextView
    android:layout_weight="1"
    android:background="#FF0000"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    />
<TextView
    android:layout_weight="2"
    android:background="#00FF00"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    />
<TextView
    android:layout_weight="3"
    android:background="#0000FF"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    />
</LinearLayout>

有我想要的结果(由android studio预览拍摄):

enter image description here

但是当我将TextView更改为View:

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

<View
    android:layout_weight="1"
    android:background="#FF0000"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    />
<View
    android:layout_weight="2"
    android:background="#00FF00"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    />
<View
    android:layout_weight="3"
    android:background="#0000FF"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    />
</LinearLayout>

布局更改(由android studio预览拍摄的屏幕):

enter image description here

是什么原因?

4 个答案:

答案 0 :(得分:1)

你必须尝试这样

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

<View
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:background="#FF0000"/>
<View
    android:layout_weight="2"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:background="#00FF00"  />
<View
    android:layout_weight="3"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:background="#0000FF"/>
</LinearLayout>

答案 1 :(得分:0)

它与宽度有关。鉴于它的水平LinearLayout匹配parent为LinearLayout的宽度,并且每个视图(View / TextView)都被赋予权重,视图的各个宽度都会导致不当行为。

如果您将每个视图宽度设置为0dp,则两者的行为方式都相同

答案 2 :(得分:0)

用0dp替换宽度。

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

    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#FF0000" />

    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="#00FF00" />

    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:background="#0000FF" />
</LinearLayout>

答案 3 :(得分:0)

您的父布局方向是水平的,因此您不能使用layout_width match_parent,wrap_content或除0以外的任何值。那么为什么它会产生不需要的结果。

有关详情,请参阅Layout Weight部分。

<View
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="#FF0000" />

<View
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2"
    android:background="#00FF00" />

<View
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="3"
    android:background="#0000FF" />

Actual output by using Wiew