2015-02-24 84 views
0

我正在嘗試做一個上學日,我使用多個linearLayout與天和小時。 隨着layout_weight我可以設置單個LinearLayout內的每個元素的空間。 但我的問題是我如何設置layout_weightLinearLayout,因爲它們設置爲水平方向,並且不佔用整個屏幕。 這就像一個表,但我使用LinearLayout,因爲我只需要創建行。Android如何採取更多的LinearLayout方向水平全屏

這是活動的XML

<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" android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context="com.ddz.diarioscolastico.OrarioActivityProva"> 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:id="@+id/linearLayout"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="20dp" 
     android:layout_weight="2" 
     android:gravity="center" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Lun" 
     android:id="@+id/textView2" 
     /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="2" 
     android:gravity="center" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Mar" 
     android:id="@+id/textView" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="2" 
     android:gravity="center" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Mer" 
     android:id="@+id/textView3" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="2" 
     android:gravity="center" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Gio" 
     android:id="@+id/textView4" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="2" 
     android:gravity="center" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Ven" 
     android:id="@+id/textView5" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="2" 
     android:gravity="center" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Sab" 
     android:id="@+id/textView6" /> 
</LinearLayout> 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:layout_below="@+id/linearLayout" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:id="@+id/linearLayout2"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="1" 
     android:id="@+id/textView7" /> 
</LinearLayout> 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/linearLayout2" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:id="@+id/linearLayout3"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="2" 
     android:id="@+id/textView8" /> 

</LinearLayout> 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/linearLayout3" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:id="@+id/linearLayout4"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="3" 
     android:id="@+id/textView9" /> 
</LinearLayout> 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/linearLayout4" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:id="@+id/linearLayout6"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="4" 
      android:id="@+id/textView10" /> 
</LinearLayout> 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/linearLayout6" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:id="@+id/linearLayout5"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="5" 
     android:id="@+id/textView11" /> 
</LinearLayout> 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/linearLayout5" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:id="@+id/linearLayout7"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="6" 
     android:id="@+id/textView12" /> 
</LinearLayout> 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/linearLayout7" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:id="@+id/linearLayout8"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="7" 
     android:id="@+id/textView13" /> 
</LinearLayout> 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/linearLayout8" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="8" 
     android:id="@+id/textView14" /> 
</LinearLayout> 

正如你可以看到,如果我設置android:layout_height="match_parent"是所有LinearLayout充分屏幕,但只有第一個是可見的,我想分割屏幕所有。

+0

'layout_weight'是LinearLayouts。將您的根元素更改爲LinearLayout。 – tachyonflux 2015-02-24 17:34:02

+0

@karaokyo謝謝你,現在我把所有'LinearLayout'放在另一個'LinearLayout'中,方向垂直,現在工作! 我可以使用'TableLayout'的''View'的權重到'TableRow'嗎? 再次感謝您。 – Mario 2015-02-24 17:45:31

回答

2

使用佈局權重是一個有點棘手的任務。 假設您想要顯示水平三個按鈕,它們在線性佈局中共享相同的寬度。那麼xml代碼應該是這樣的。

<?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:weightSum="3" 
    android:orientation="horizontal" > 
    <Button 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:text="Button 1" 
     /> 
    <Button 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:text="Button 1" 
     /> 
    <Button 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:text="Button 1" 
     /> 

</LinearLayout> 

正如可以看到,在父的LinearLayout我用weightSum作爲3和在每個子按鈕i設置寬度0dp和重量爲1,並且有三個按鈕,因此將分享相同的寬度。

順便說一下,在給定的佈局中,根父(RelativeLayout)沒有關閉。

編輯:2

正如你這裏說的是代碼將每個LinearLayout中與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:weightSum="3" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:layout_width="match_parent" 
     android:background="#00FF00" 
     > 

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

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

    </LinearLayout> 

</LinearLayout> 

輸出同樣: OutPut

+0

我忘了複製它!感謝您的答覆,但我需要垂直分隔所有LinearLayout的屏幕。 如果我把所有的LinearLayout放在另一個LinearLayout中,我可以設置其他的權重。 – Mario 2015-02-24 17:55:14

+0

絕對可以使用重量來設置子LinearLayout。 – theapache64 2015-02-24 18:00:35

+0

嗨達里奧,我已經更新了答案,請檢查它 – theapache64 2015-02-24 18:08:42

0

您是否嘗試用TableRow替換LinearLayout或使用GridLayout? GridLayout需要API 14,但較低的API可以與支持庫一起使用。