Android如何使用更多LinearLayout方向水平全屏

时间:2015-02-24 17:22:01

标签: android android-linearlayout

我正在尝试做一个上学日,我使用多个linearLayout几天和几小时。 使用layout_weight,我可以为单个LinearLayout内的每个元素设置空间。 但我的问题是如何为layout_weight设置LinearLayout,因为它们设置为水平方向,而不是整个屏幕。 它就像一个表,但我使用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全屏,但只有第一个可见,我想将屏幕分开。

2 个答案:

答案 0 :(得分:3)

使用布局权重是一项有点棘手的任务。 假设您想要在线性布局中水平显示三个相等宽度的按钮。然后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,在每个子按钮中我将宽度设置为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

答案 1 :(得分:0)

您是否尝试使用TableRow替换LinearLayout或使用GridLayout? GridLayout需要API 14,但较低的API可以与支持库一起使用。