2014-2-2android佈局管理器3

1.    網格佈局GridLayout

網格佈局GridLayout是Android4.0新增佈局管理器。它將整個容器劃分爲rows×columns個網格。每個網格可以放置一個組件,也可設置一個組件橫跨多少列(行)。

實例:計算機界面

Xml代碼清單:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:rowCount="6"

    android:columnCount="4"

    android:id="@+id/root"

    >

    <!-- 定義一個橫跨4列的文本框,

    並設置該文本框的前景色、後景色等屬性 -->

<TextView

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_columnSpan="4"

    android:textSize="50sp"

    android:layout_marginLeft="4px"

    android:layout_marginRight="4px"

    android:padding="5px"

    android:layout_gravity="right"

    android:background="#eee"

    android:textColor="#000"

    android:text="0"

    />      

    <!-- 定義一個橫跨4列的按鈕 -->

<Button

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_columnSpan="4"

    android:text="消除"/>

</GridLayout>

JAVA代碼清單:

packagecom.hqsA.gridlayouttext;

 

importandroid.os.Bundle;

import android.app.Activity;

importandroid.view.Gravity;

importandroid.widget.Button;

importandroid.widget.GridLayout;

 

public classGridLayoutText extends Activity {

 

         //定義16個按鈕的文本

         String[] chars = new String[]

                            {

                                     "7","8","9","÷",

                                     "4","5","6","×",

                                     "1","2","3","-",

                                     ".","0","=","+",

                            };

         @Override

         protected void onCreate(BundlesavedInstanceState) {

                   super.onCreate(savedInstanceState);

                   setContentView(R.layout.grid_layout_text);

                   GridLayout gridLayout =(GridLayout) findViewById(R.id.root);

                  

                   for(int i = 0 ; i <chars.length ; i++ )

                   {

                            Button bn = newButton(this);

                            bn.setText(chars[i]);

                            //設置該按鈕的字號大小

                            bn.setTextSize(40);

                            //設置該組件所在的行

                            GridLayout.SpecrowSpec = GridLayout.spec(i / 4 + 2);

                            //指定該組件所在的列

                            GridLayout.SpeccolumnSpec = GridLayout.spec(i % 4);

                            GridLayout.LayoutParamsparams = new GridLayout.LayoutParams(

                                               rowSpec, columnSpec);

                            //指定該組件佔滿父容器

                            params.setGravity(Gravity.FILL);

                            gridLayout.addView(bn,params);

                   }

         }

}

效果圖:

 

2.    絕對佈局AbsoluteLayout

Android不提供任何佈局控制,有開發人員自己通過X座標、Y座標來控制組件位置。

實例:登錄界面

Xml代碼清單:

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

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

    <!-- 定義一個文本框,使用絕對定位 -->

    <TextView

         android:layout_x="20dip"

         android:layout_y="20dip"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:text="用戶名:"

         />

    <!-- 定義一個文本編輯框,使用絕對定位 -->

    <EditText

        android:layout_x="80dip"

        android:layout_y="15dip"

        android:layout_width="wrap_content"

         android:width="200px"

        android:layout_height="wrap_content"

 

        />

    <!-- 定義一個文本框,使用絕對定位 -->

    <TextView

         android:layout_x="20dip"

         android:layout_y="80dip"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:text=" 碼:"

         />

    <!-- 定義一個文本編輯框,使用絕對定位 -->

    <EditText

        android:layout_x="80dip"

        android:layout_y="75dip"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

         android:width="200px"

         android:password="true"

        />

    <!-- 定義一個文本編輯框,使用絕對定位 -->

    <Button

        android:layout_x="130dip"

        android:layout_y="135dip"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text=""

        />

</AbsoluteLayout>

JAVA代碼清單(略)

Ps:在手機中查看效果是出現了一下情況。

       查找原因才發現在xml代碼中有些字拼寫出錯!而eclipse沒有報錯!

       所以以後讓要注意編寫軟件不報錯並不代表程序代碼沒有錯誤,仍然可能存在程序代碼編寫錯誤!

          改正後:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章