`

Android系统开发02—Android布局管理器

阅读更多
Android系统开发02—Android布局管理器

1. View,ViewGroup
1) View类
View类为所有可视化控件的基类,主要提供了空间绘制和事件处理。
2)ViewGroup类
ViewGroup类也是View类的子类,但是可以充当其他控件的容器。

2. 线性布局
线性布局是最简单的布局,它提供了控件水平或者垂直排列的模型。使用此布局时可以通过设置控件的weight参数控制各个控件在容器中的相对大小。
线性布局实例:
string.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">LinearExample</string>
    <string name="button">按钮</string>
    <string name="add">添加</string>
</resources>

main.xml:
<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/lla" android:orientation="vertical" android:gravity="right">
        <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/Button01" android:text="@string/add"></Button>
    </LinearLayout>

MyAndroidProject.java:
package qijia.si;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;


public class MyAndroidProject extends Activity {
	int count = 0;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.main);
        Button btn = (Button) findViewById(R.id.Button01);
        
        btn.setOnClickListener(
        		new View.OnClickListener() {
					
					public void onClick(View v) {
						// TODO Auto-generated method stub
						LinearLayout ll = (LinearLayout) findViewById(R.id.lla);
						
						String msg = MyAndroidProject.this.getResources().getString(R.string.button);
						Button tempbutton = new Button(MyAndroidProject.this);
						
						tempbutton.setText(msg+(++count));
						tempbutton.setWidth(80);
						ll.addView(tempbutton);
						
					}
				}
        
        
        
        
        );
        
          
    }
}
3. 表格布局
TableLayout类以行和列的形式管理控件,每行为一个TableRow对象,也可以为一个View对象,当为View对象时,该View对象将跨越该行的所有列。在TableRow中可以添加子空间,每添加一个子空间为一列。
在TableLayout中,可以为列设置三种属性:
1) Shrinkable,该列宽度可以进行收缩,以使表格能够适应期父容器的大小
2) Stretchable,该列的宽度可以进行拉伸,以使填满表格中空闲的空间。
3) Collapsed,该列将被隐藏。
注意:一个列可以同时拥有1,2的属性。
案例:
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="white">#ffffff</color>
    <color name="red">#ff0000</color>
    <color name="black">#000000</color>
    <color name="green">#00ff00</color>
    <color name="blue">#0000ff</color>
</resources>

string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">TableExample</string>
    <string name="tv1">我自己是一行…………我自己是一行</string>
    <string name="tvShort">我的内容少</string>
    <string name="tvStrech">我是被拉伸的一行</string>
    <string name="tvShrink">我是被收缩的一行被收缩的一行</string>
    <string name="tvLong">我的内容很多很多很多很多很多很多</string>
</resources>

main.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@drawable/pic" android:gravity="bottom">
        <TableLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/TableLayout03" android:background="@color/white" android:stretchColumns="0" android:collapseColumns="1">
        </TableLayout>
        <TableLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/TableLayout02" android:background="@color/white" android:stretchColumns="0">
            <TableRow android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/TableRow01">
                <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/TextView02" android:text="@string/tvStrech" android:layout_margin="4px" android:background="@color/green" android:textColor="@color/blue"></TextView>
                <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/TextView03" android:text="@string/tvShort" android:textColor="@color/black" android:background="@color/blue" android:layout_margin="4px"></TextView>
            </TableRow>
        </TableLayout>
        <TableLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/TableLayout01" android:background="@color/white">
            <TextView android:textColor="@color/black" android:layout_margin="4px" android:text="@string/tv1" android:background="@color/red" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/TextView01"></TextView>
        </TableLayout>
    </LinearLayout>

MyAndroidProject.java
package qijia.si;

import android.app.Activity;
import android.os.Bundle;



public class MyAndroidProject extends Activity {
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.main);
          
    }
}

4. 相对布局
在相对布局中,子控件的位置是相对兄弟控件或父容器而决定的。
需要注意的是,相对布局中要避免出现循环依赖。
5. 帧布局
FrameLayout帧布局在屏幕上开辟出了一块区域,在这块区域中可以添加多个子控件,但是所有的子空间都被对齐到屏幕的左上角。帧布局的大小由子控件中尺寸最大的那个控件决定。
6. 绝对布局
绝对布局是指屏幕中所有控件的摆放由开发人员通过设置控件的坐标来指定,控件容器不再负责管理其子控件的位置。
案例:
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">AbsoluteExample</string>
    <string name="uid">用户名</string>
    <string name="pwd">密码</string>
    <string name="ok">确定</string>
    <string name="cancel">取消</string>
</resources>

color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="white">#ffffff</color>
    <color name="red">#ff0000</color>
    <color name="black">#000000</color>
    <color name="green">#00ff00</color>
    <color name="blue">#0000ff</color>
</resources>

main.xml
<?xml version="1.0" encoding="utf-8"?>
    <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:background="@color/white" android:id="@+id/absoluteLayout1" android:layout_height="fill_parent">
        <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_x="20dip" android:layout_y="20dip" android:id="@+id/TextView01" android:text="@string/uid"></TextView>
        <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/TextView02" android:text="@string/pwd" android:layout_x="20dip" android:layout_y="80dip"></TextView>
        <EditText android:text="EditText" android:layout_height="wrap_content" android:layout_y="20dip" android:id="@+id/EditText01" android:layout_x="80dip" android:layout_width="180dip"></EditText>
        <EditText android:text="EditText" android:layout_height="wrap_content" android:id="@+id/EditText02" android:password="true" android:layout_x="80dip" android:layout_y="80dip" android:layout_width="180dip"></EditText>
        <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/Button01" android:text="@string/ok" android:layout_x="155dip" android:layout_y="140dip"></Button>
        <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/Button02" android:text="@string/cancel" android:layout_x="210dip" android:layout_y="140dip"></Button>
        <ScrollView android:layout_x="10dip" android:layout_y="200dip" android:layout_height="150dip" android:layout_width="250dip" android:id="@+id/ScrollView01">
            <EditText android:text="EditText" android:layout_height="wrap_content" android:layout_width="fill_parent" android:singleLine="false" android:gravity="top" android:id="@+id/EditText03"></EditText>
        </ScrollView>
    </AbsoluteLayout>

MyAndroidProject.java
package qijia.si;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class MyAndroidProject extends Activity {
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.main);
        final Button okButton = (Button) findViewById(R.id.Button01);
        final Button cancelButton = (Button) findViewById(R.id.Button02);
        
        final EditText uid = (EditText) findViewById(R.id.EditText01);
        final EditText pwd = (EditText) findViewById(R.id.EditText02);  
        final EditText log = (EditText) findViewById(R.id.EditText03);
        
        okButton.setOnClickListener(
        			new View.OnClickListener() {
						
						public void onClick(View v) {
							// TODO Auto-generated method stub
							String uidStr = uid.getText().toString();
							String pwdStr = pwd.getText().toString();
							
							log.append("用户名:"+uidStr+"密码:"+pwdStr+"\n");
							
						}
					}
        
        );
        
        cancelButton.setOnClickListener(
        			new View.OnClickListener() {
						
						public void onClick(View v) {
							// TODO Auto-generated method stub
							uid.setText("");
							pwd.setText("");
						}
					}
        
        
        );
        
    
    
    }
}
分享到:
评论

相关推荐

    Android开发——布局管理

    Android开发——布局管理 博文地址: https://blog.csdn.net/Zach_z/article/details/80587712

    Android开发登陆界面布局

    Android开发登陆界面布局

    android应用开发范例精解

    第2篇为应用开发篇,通过实例介绍了Android UI布局、Android人机界面、手机硬件设备的使用、Android本地存储系统、Android中的数据库、多线程设计、Android传感器、Android游戏开发基础、Android与Internet,以及...

    android开发揭秘PDF

    第1章 Android开发简介 1.1 Android基本概念 1.1.1 Android简介 1.1.2 Android的系统构架 1.1.3 Android应用程序框架 1.2 OMS介绍 1.2.1 OPhone介绍 1.2.2 Widget介绍 1.3 小结 第2章 Android开发环境搭建 2.1 ...

    android studio实现安卓移动开发课程设计通讯录管理系统源代码.zip

    安卓移动开发课程设计通讯录管理系统源代码 使用说明: 使用android studio 导入项目,修改文件定制自己想要的布局风格以及功能 如果导入项目后报错,哪么自己先创建好项目,将下载好的项目的代码进行复制 Phone、...

    Android 相对布局实例

    Android 相对布局实例 ,RalateLayout

    基于android的手机列车订票系统开发(带论文报告)

    本次实验目的就是通过开发火车票订票系统,实现这些app网上购票的整个流程,在开发过程中逐步熟悉使用android studio、IDEA等主流开发工具,对android常规布局设计、常用的组件和相关的事件处理有更深入的了解,...

    Android开发 - 布局

    NULL 博文链接:https://zzqrj.iteye.com/blog/1124604

    老罗android开发视频教程全集百度网盘下载

    【第一版第二章】老罗Android开发视频--常用UI布局介绍(5集) 【第一版第三章】老罗Android开发视频--HTTP协议编程(4集) 【第一版第四章】老罗Android开发视频--解析XML数据(3集) 【第一版第五章】老罗...

    老罗Android视频开发教程 android常用布局的介绍代码

    老罗Android视频开发教程 android常用布局的介绍代码,很不错的教程代码。

    Android 系统开发技术课程设计 实验任务书及实验报告

    资源包含十一个实验...实验4:布局管理器 实验5:Intent、Activity应用 实验6:基本视图组件的使用(三) 实验7 音乐播放器 实验8service应用 实验9 设置Alarm 实验10 基于SQLite的通信录 实验11 基于文件的日程安排

    Android开发之简单文件管理器实现方法

    本文实例讲述了Android开发之简单文件管理器实现方法。分享给大家供大家参考,具体如下: 这里运用Java I/O、ListActivity、Dialog、Bitmap等实现简单文件管理器,可以查看目录文件,修改文件名,删除文件,打开文件...

    Android开发模仿京东app的布局Demo源代码

    Android开发模仿京东APP的布局源代码

    android手机短信管理系统论文 完整版

    4.1.3 更新Ec1ipse并下载ADT(Android开发插件) 19 4.1.4 使用Ec1ipse开发Android应用 20 4.2 短信管理软件程序中各类组件 21 4.2.1 Package Explorer中的组件 22 4.2.2 应用程序的资源 22 4.2.3 布局文件main.xml 22...

    Android 五大布局方式详解

    Android中常用的5大布局方式有以下几种:...线性布局是Android开发中最常见的一种布局方式,它是按照垂直或者水平方向来布局,通过“android:orientation”属性可以设置线性布局的方向。属性值有垂直(vertical)和水平

    《Android应用开发揭秘》附带光盘代码.

     第1章 Android开发简介  1.1 Android基本概念  1.1.1 Android简介  1.1.2 Android的系统构架  1.1.3 Android应用程序框架  1.2 OMS介绍  1.2.1 OPhone介绍  1.2.2 Widget介绍  1.3 小结  第2章 Android...

    android 常用控件布局汇总

    android 常用控件如:ProgressBar、ActionBar、Switch、ListView 、PopupWindow....等的用法,各种Style的写法以及自定义常用控件 android 五大布局的汇总及用法

    Android开发——布局方式Demo源码

    Android布局详解实例,包含:线性布局(LinearLayout)、相对布局(RelativeLayout)、帧布局(FrameLayout)、表格布局(TableLayout)四大布局方式的demo

    Android实例之奖牌表(布局嵌套AndroidStudio2.2开发)

    这是一个Android入门的笑程序,运用了布局嵌套,用AndroidStudio2.2开发的,有需要的小伙伴可以下载

    Android开发教程(完整版)

    新版Android开发教程+笔记六--应用3、4 布局.pdf 新版Android开发教程+笔记七--基础UI编程1.pdf 新版Android开发教程+笔记八--基础UI编程2.pdf 新版Android开发教程+笔记九--基础UI编程3.pdf 新版Android开发教程+...

Global site tag (gtag.js) - Google Analytics