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

来源:互联网 发布:数控钻铣床编程 编辑:程序博客网 时间:2024/06/06 01:45

1.    网格布局GridLayout

网格布局GridLayout是Android4.0新增布局管理器。它将整个容器划分为rows×columns个网格。每个网格可以放置一个组件,也可设置一个组件横跨多少列(行)。

实例:计算机界面

Xml代码清单:

<GridLayoutxmlns: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代码清单:

<?xmlversion="1.0"encoding="utf-8"?>

<AbsoluteLayoutxmlns: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没有报错!

       所以以后让要注意编写软件不报错并不代表程序代码没有错误,仍然可能存在程序代码编写错误!

          改正后:

 

0 0
原创粉丝点击