2010-08-06 74 views
52

想用两个LinearLayouts为我的应用程序分割一个屏幕。我应该使用哪些参数来精确分割两个相等的部分 - 第一个是LinearLayout,第二个是正下方。如何用两个相等的LinearLayout分割屏幕?

+0

对每个布局使用权重= 0.5 – Sephy 2010-08-06 15:10:33

+2

两个布局的权重应该是“相同的”,不一定是分数 – Siddharth 2013-07-05 03:24:58

回答

105

使用重量参数,大致的布局将类似于这样:

<LinearLayout android:orientation="horizontal" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"> 

<LinearLayout 
    android:layout_weight="1" 
    android:layout_height="fill_parent" 
    android:layout_width="0dp"/> 

<LinearLayout 
    android:layout_weight="1" 
    android:layout_height="fill_parent" 
    android:layout_width="0dp"/> 

</LinearLayout> 
+0

您拼写了第3个LinearLayout错误。 – Doomsknight 2012-09-05 13:16:08

+0

@Doomsknight thx,修正! – 2012-09-09 07:53:58

+2

查看本教程中有关使用layout_weight属性的信息http://www.chess-ix.com/2012/01/17/the-use-of-layout_weight-with-android-layouts/ – 2012-09-24 18:19:43

11

只是把它在那里:

<?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:background="#FF0000" 
    android:weightSum="4" 
    android:padding="5dp"> <!-- to show what the parent is --> 
    <LinearLayout 
     android:background="#0000FF" 
     android:layout_height="0dp" 
     android:layout_width="match_parent" 
     android:layout_weight="2" /> 
    <LinearLayout 
     android:background="#00FF00" 
     android:layout_height="0dp" 
     android:layout_width="match_parent" 
     android:layout_weight="1" /> 
</LinearLayout> 
35

我后4-5年,但这样做的最佳实践回答这个问题这是下面

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 

    <LinearLayout 
     android:id="@+id/firstLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_toLeftOf="@+id/secondView" 
     android:orientation="vertical"></LinearLayout> 

    <View 
     android:id="@+id/secondView" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_centerHorizontal="true" /> 

    <LinearLayout 
     android:id="@+id/thirdLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_toRightOf="@+id/secondView" 
     android:orientation="vertical"></LinearLayout> 
</RelativeLayout> 

这是正确的做法是使用layout_的对于UI操作来说,重量总是很重的。 使用LinearLayout同等地分割布局不是好的做法

+0

真的很棒的回答:) – Arlind 2015-06-06 11:37:22

+0

就像一个魅力。最好的状态回答 – MPhil 2016-08-25 08:09:39

+0

这应该是正确的答案。 – 2017-01-31 10:51:22