开发者

Android LinearLayout

I am sooo stumped right now trying to get the layout that I want...I just started with android and I am playing around with the different layout options. So, as an experiment, I want to create a layout that looks like

---------
|_______|
|   |   |
|   |   |
|   |   |
---开发者_Python百科------

This is what I have so far and what i get is 3 columsn with the leftmost one being the thinnest column.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/firstlayout"
              android:layout_height="fill_parent"
              android:layout_width="fill_parent">

    <LinearLayout android:layout_height="fill_parent"
                  android:layout_width="0px"
                  android:layout_weight="0.1"
                  android:background="#0000FF"
                  android:orientation="horizontal"
                  android:gravity="top">

        <TextView android:layout_height="fill_parent"
                  android:layout_width="fill_parent"
                  android:text="Top Bar"
                  android:layout_gravity="center"/>

    </LinearLayout>


    <LinearLayout android:layout_height="fill_parent"
                  android:layout_width="0px"
                  android:layout_weight="0.9"
                  android:orientation="horizontal"
                  android:gravity="bottom">

        <LinearLayout android:layout_height="fill_parent"
                      android:layout_width="0px"
                      android:layout_weight="0.5"
                      android:background="#FF0000">

            <TextView android:layout_height="fill_parent"
                      android:layout_width="fill_parent"
                      android:gravity="center"
                      android:text="Bottom Left"/>

        </LinearLayout>
        <LinearLayout android:layout_height="fill_parent"
                      android:layout_width="0px"
                      android:layout_weight="0.5"
                      android:background="#00FF00">

            <TextView android:layout_height="fill_parent"
                      android:layout_width="fill_parent"
                      android:gravity="center"
                      android:text="Bottom Right"/>

        </LinearLayout>
    </LinearLayout>


</LinearLayout>

Can someone tell me what I am doing wrong here?


What you need is a two-level LinearLayout. Try this:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <!--  Views for the top row go here -->
    </LinearLayout>

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

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

            <!-- Views for the left column go here -->
        </LinearLayout>

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

            <!-- Views for the right column go here -->
        </LinearLayout>
    </LinearLayout>
</LinearLayout>


You can use RelativeLayout.

 <RelativeLayout
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">

 <TextView
  android:id="@+id/Toptext1"
   android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  >

 <TextView
   android:id="@+id/LeftText2"
   android:layout_width="wrap_content"
  android:layout_height="fill_parent"
  android:layout_below="@id/Toptext1"/>

   <TextView
   android:id="@+id/RightText3"
   android:layout_width="wrap_content"
  android:layout_height="fill_parent"
  android:layout_below="@id/Toptext1"
  android:layout_toRightOf="@id/LeftText2"/>
</RelativeLayout>


To break down the layout, you should set android:orientation="vertical"

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/firstlayout"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
  >

<LinearLayout
    android:layout_height="0dp"
    android:layout_width="fill_parent"
    android:layout_weight="0.1"
    android:background="#0000FF"
    android:gravity="top"
>
    <TextView
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:text="Top Bar"
        android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:layout_weight="0.9"
    android:gravity="bottom"
>
    <LinearLayout
        android:layout_height="fill_parent"
        android:layout_width="0px"
        android:layout_weight="0.5"
        android:background="#FF0000"
    >
        <TextView
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:gravity="center"
            android:text="Bottom Left" />
    </LinearLayout>
    <LinearLayout
        android:layout_height="fill_parent"
        android:layout_width="0px"
        android:layout_weight="0.5"
        android:background="#00FF00"
    >
        <TextView
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:gravity="center"
            android:text="Bottom Right" />
    </LinearLayout>
</LinearLayout>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜