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没有报错!
所以以后让要注意编写软件不报错并不代表程序代码没有错误,仍然可能存在程序代码编写错误!
改正后:
- 2014-2-2android布局管理器3
- 2014-2-1android布局管理器2
- Android开发5:布局管理器2(表格布局TableLayout)
- 2014-1-31android布局管理器
- android 布局管理器
- Android布局管理器详解
- Android布局管理器
- Android-布局管理器
- Android布局管理器
- android布局管理器学习
- Android中的布局管理器
- android 布局管理器
- android布局管理器
- 关于android布局管理器
- Android布局管理器介绍
- android布局管理器
- android布局管理器
- Android布局管理器浅析
- Android开发优化-布局优化
- Android中AutoCompleteTextView完整示例(二)
- Effective C++(17) 以独立语句将newed对象置入智能指针
- Android中AutoCompleteTextView完整示例(一)
- MFC 常见错误及解决方法(日积月累)
- 2014-2-2android布局管理器3
- ListView下拉刷新,上拉自动加载更多
- 数据库以及线程发生死锁的原理及必要条件,如何避免死锁。
- HDU--杭电--1532--Drainage Ditches--最大流
- scala学习及Intellij Idea
- TMX1.4b规范概述
- 数据结构与算法学习笔记01(引言)
- MySQL连接无法解析HOST主机名
- UVa:10912 Simple Minded Hashing