2010-08-06 85 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