2017-01-17 46 views
2

这个。我不明白为什么第一个布局看起来比第二个布局要大,如果最后一个布局的0.6(60%)。layout_weight =“。4”看起来比layout_weight =“。6”大

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight=".4" > 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight=".6"> 
    </RelativeLayout> 
</LinearLayout> 
+1

它的工作反转,.4大于.6。 –

回答

1

@Miguel巴拉适用于本

android:layout_width="match_parent" 

更换到

android:layout_width="wrap_content" 

android:layout_width="0dp" 

在两个相对布局

0

老实说,我没有尝试使用分数权重值,但理论上他们应该工作。但是我想这里的问题是,你需要设置layout_width两个Relativelayouts都是0dp。只有权重参数才会生效。试一试。 从机器人文档 -

为了创建在其中每个子使用在屏幕上的 空间将同样量的线性布局,设定机器人:每个视图的layout_height到 “0dp”(用于垂直布局)或者将每个视图的android:layout_width设置为“0dp”(对于水平布局)。然后将每个视图的android:layout_weight 设置为“1”。

0

你应该有这个。

,如果你使用的是重量

情况下1有两种情况:如果你有水平的父母。你应该有layout_width =“0dp”

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="horizontal" android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight=".6" > 

     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight=".4"> 
     </RelativeLayout> 
    </LinearLayout> 

情况1:如果你有垂直父母。你应该有layout_height = “0dp”

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="1"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".6" > 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight=".4"> 
    </RelativeLayout> 
</LinearLayout> 
0

LinearLayout(父视图)设置layout_height = "0dp"

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

<RelativeLayout 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight=".6" > 

</RelativeLayout> 

<RelativeLayout 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight=".4"> 
</RelativeLayout> 
</LinearLayout> 

anotherway是添加你childview重量总和值(0.6 + 0.4 = 1)

android:weightSum="1" 

in LinearLayout

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