Android入门之布局篇.docx

上传人:b****6 文档编号:6555224 上传时间:2023-01-07 格式:DOCX 页数:19 大小:171.23KB
下载 相关 举报
Android入门之布局篇.docx_第1页
第1页 / 共19页
Android入门之布局篇.docx_第2页
第2页 / 共19页
Android入门之布局篇.docx_第3页
第3页 / 共19页
Android入门之布局篇.docx_第4页
第4页 / 共19页
Android入门之布局篇.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

Android入门之布局篇.docx

《Android入门之布局篇.docx》由会员分享,可在线阅读,更多相关《Android入门之布局篇.docx(19页珍藏版)》请在冰豆网上搜索。

Android入门之布局篇.docx

Android入门之布局篇

Android简单入门

最近Android挺火的,可惜刚毕业,温饱才刚刚解决,还没能力买台Android手机,所以目前的开发只能用模拟器来做。

就目前AndroidSDK1.5+Eclipse+ADT的开发方式来说,跟J2ME最大的区别在于UI的不同,当然Android比J2ME多出很多东西,多出的是J2ME无法作对比的。

刚开始做Android开发,很多人都是先写个简单的界面,再加点控制代码,本文就是这样。

  本文所讲到的是LinearLayout+Button+EditText+AlertDialog的简单使用。

Activity以LinearLayout排列,共用到两个LinearLayout,第一个是用于全窗体,第二个用于存放两个Button,第二个LinearLayout放在EditText控件下面,以下给出main.xml的代码:

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

>

xmlns:

android="

android:

orientation="vertical"

android:

layout_width="fill_parent"

android:

layout_height="fill_parent"

>

android:

text="EditText01"

android:

layout_height="wrap_content"

android:

layout_width="fill_parent"

android:

id="@+id/edtInput">

android:

id="@+id/LinearLayout01"

android:

layout_height="wrap_content"

android:

layout_width="fill_parent"

android:

gravity="center"

>

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="Show"

android:

id="@+id/btnShow">

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="Clear"

android:

id="@+id/btnClear">

  main.xml用于Activity的UI设计,目前设计起来的速度,比J2ME上的LWUIT略快(两者类似,Android提供了GUI设计工具),比WM上的.NETCF略慢(.NETCF是RAD)。

接下来给出JAVA代码:

packagecom.studio.android;

importandroid.app.Activity;

importandroid.app.AlertDialog;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importandroid.widget.EditText;

publicclassHelloAndroidextendsActivity{

/**Calledwhentheactivityisfirstcreated.*/

ButtonbtnShow;

ButtonbtnClear;

EditTextedtInput;

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

btnShow=(Button)findViewById(R.id.btnShow);//控件与代码绑定

btnClear=(Button)findViewById(R.id.btnClear);//控件与代码绑定

edtInput=(EditText)findViewById(R.id.edtInput);//控件与代码绑定

btnShow.setOnClickListener(newClickListener());//使用点击事件

btnClear.setOnClickListener(newClickListener());//使用点击事件

}

classClickListenerimplementsOnClickListener{

publicvoidonClick(Viewv){

if(v==btnShow){

newAlertDialog.Builder(HelloAndroid.this)

.setIcon(android.R.drawable.ic_dialog_alert)

.setTitle("Information")

.setMessage(edtInput.getText())

.show();

}

elseif(v==btnClear){

edtInput.setText("HelloAndroid");

}

}

}

}

刚开始Android的开发,界面设计是J2ME程序员的瓶颈之处,不过以后Android的开发工具会越来越智能化,期待Netbeans推出更好的ADT出来(Netbeans目前已经有Android插件)。

LinearLayout、AbsoluteLayout

Android的UI布局都以Layout作为容器,在上面按照规定排列控件,这方面跟JAVA的Swing和LWUIT很像。

控件跟Layout有很多属性是一样的,可以在Properties里面修改,跟.NET/Delphi等RAD类似,其中最常用的属性有以下这些:

  id="@+id/edtInput",ID是连接UI与代码的桥梁

  gravity="center",Layout中的控件居中

  layout_width="fill_parent",自动填充至屏幕宽度,layout_height同理

  layout_width="wrap_content",自动填充为控件大小,layout_height同理

  LinearLayout很简单:

在LinearLayout里面的控件,按照水平或者垂直排列:

orientation="horizontal":

水平排列;

orientation="vertical":

垂直排列

  当LinearLayout是horizontal,并且里面的控件使用了layout_width="fill_parent",第二组控件会挡在屏幕的右边,那也就是看不到了。

  AbsoluteLayout,是一个按照绝对坐标定义的布局,由于使用绝对坐标去定位控件,因此要实现自适应界面时,应尽少使用AbsoluteLayout。

AbsoluteLayout里面的控件都以layout_x、layout_y来定义其位置:

  上图中的TextView01的X坐标为10px,Y坐标为10px:

android:

id="@+id/AbsoluteLayout01"

android:

layout_height="wrap_content"

android:

layout_width="fill_parent">

android:

text="TextView01"

android:

id="@+id/TextView01"

android:

layout_height="wrap_content"

android:

layout_y="10px"

android:

layout_width="wrap_content"

android:

layout_x="110px">

RelativeLayout、FrameLayout

RelativeLayout是一个按照相对位置排列的布局,跟AbsoluteLayout这个绝对坐标布局是个相反的理解。

  在RelativeLayout布局里的控件包含丰富的排列属性:

  Layoutabove:

选择IDA,则该控件在A控件的上方,Layoutbelow、Layouttoleftof。

等同样用法。

使用RelativeLayout布局的时候,最好在界面设计时做好布局,尽少程序运行时做控件布局的更改,因为RelativeLayout布局里面的属性之间,很容易冲突,例如,Layoutbelow、Layoutabove同选IDA,那就肯定发生冲突了。

  FrameLayout,顾名思义跟帧有关,布局里所有的控件都被放到布局的左上角,并且一层覆盖一层。

  FrameLayout布局里面的控件布局属性才那几项,其中关键的是layout_gravity,负责控制控件的位置。

  FrameLayout布局常用在哪些情况目前我也不太了解,钻研中……

TableLayout

TableLayout跟TableLayout是一组搭配使用的布局,TableLayout置底,TableRow在TableLayout的上面,而Button、TextView等控件就在TableRow之上,另外,TableLayout之上也可以单独放控件。

TableLayout是一个使用复杂的布局,最简单的用法就仅仅是拖拉控件做出个界面,但实际上,会经常在代码里使用TableLayout,例如做出表格的效果。

  TableLayout经常用的属性是:

  android:

collapseColumns:

以第0行为序,隐藏指定的列:

  android:

collapseColumns该属性为空时,如下图:

  把android:

collapseColumns=0,2--------------》意思是把第0和第2列去掉,如下图:

  android:

shrinkColumns:

以第0行为序,自动延伸指定的列填充可用部分:

  当LayoutRow里面的控件还没有布满布局时,shrinkColumns不起作用,如下图:

  设置了shrinkColumns=0,1,2,布局完全没有改变,因为LayoutRow里面还剩足够的空间。

  当LayoutRow布满控件时,如下图:

  设置了shrinkColumns=2,则结果如下图,控件自动向垂直方向填充空间:

  android:

stretchColumns:

以第0行为序,尽量把指定的列填充空白部分:

  设置stretchColumns=1,则结果如下图,第1列被尽量填充(Button02与TextView02同时向右填充,直到TextView03被压挤到最后边)。

  Android的TableLayout+TableRow虽然使用有点复杂,但是功能很强大。

Android提供了很多布局属性,但是手机程序的界面没有PC那么花俏,所以常用的就那几项而已。

main.xml的代码如下,用到TableLayout的ID为TableLayout01:

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

>

android="

android:

orientation="vertical"

android:

layout_width="fill_parent"

android:

layout_height="fill_parent"

>

android:

id="@+id/TableLayout01"

android:

layout_width="fill_parent"

android:

layout_height="wrap_content">

JAVA代码如下:

packagecom.LayoutDemo;

importcom.LayoutDemo.R;

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.view.ViewGroup;

importandroid.widget.TableLayout;

importandroid.widget.TableRow;

importandroid.widget.TextView;

publicclassLayoutDemoextendsActivity{

/**Calledwhentheactivityisfirstcreated.*/

privatefinalintWC=ViewGroup.LayoutParams.WRAP_CONTENT;

privatefinalintFP=ViewGroup.LayoutParams.FILL_PARENT;

@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

//新建TableLayout01的实例

TableLayouttableLayout=(TableLayout)findViewById(R.id.TableLayout01);

//全部列自动填充空白处

tableLayout.setStretchAllColumns(true);

//生成10行,8列的表格

for(introw=0;row<10;row++)

{

TableRowtableRow=newTableRow(this);

for(intcol=0;col<8;col++)

{

//tv用于显示

TextViewtv=newTextView(this);

tv.setText("("+col+","+row+")");

tableRow.addView(tv);

}

//新建的TableRow添加到TableLayout

tableLayout.addView(tableRow,newTableLayout.LayoutParams(FP,WC));

}

}

}

结果如下图:

ListView

ListView是一个经常用到的控件,ListView里面的每个子项Item可以使一个字符串,也可以是一个组合控件。

先说说ListView的实现:

  1.准备ListView要显示的数据;

  2.使用一维或多维动态数组保存数据;

  2.1构建适配器,简单地来说,适配器就是Item数组,动态数组有多少元素就生成多少个Item;

  3.把适配器添加到ListView,并显示出来。

  接下来,看看本文代码所实现的ListView:

  接下来,就开始UI的XML代码:

  main.xml代码如下,很简单,也不需要多做解释了:

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

>

android:

id="@+id/LinearLayout01"

android:

layout_width="fill_parent"

android:

layout_height="fill_parent"

xmlns:

android="

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

id="@+id/MyListView">

my_listitem.xml的代码如下,my_listitem.xml用于设计ListView的Item:

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

>

android:

layout_width="fill_parent"

xmlns:

android="

android:

orientation="vertical"

android:

layout_height="wrap_content"

android:

id="@+id/MyListItem"

android:

paddingBottom="3dip"

android:

paddingLeft="10dip">

android:

layout_height="wrap_content"

android:

layout_width="fill_parent"

android:

id="@+id/ItemTitle"

android:

textSize="30dip">

android:

layout_height="wrap_content"

android:

layout_width="fill_parent"

android:

id="@+id/ItemText">

解释一下,里面用到的一些属性:

  1.paddingBottom="3dip",Layout往底部留出3个像素的空白区域

  2.paddingLeft="10dip",Layout往左边留出10个像素的空白区域

  3.textSize="30dip",TextView的字体为30个像素那么大。

  最后就是JAVA的源代码:

 

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

//绑定XML中的ListView,作为Item的容器

ListViewlist=(ListView)findViewById(R.id.MyListView);

//生成动态数组,并且转载数据

ArrayList>mylist=newArrayList>();

for(inti=0;i<30;i++)

{

HashMapmap=newHashMap();

map.put("ItemTitle","ThisisTitle.....");

map.put("ItemText","Thisistext.....");

mylist.add(map);

}

//生成适配器,数组===》ListItem

SimpleAdaptermSchedule=newSimpleAdapter(this,//没什么解释

mylist,//数据来源

R.layout.my_listitem,//ListItem的XML实现

//动态数组与ListItem对应的子项

newString[]{"ItemTitle","ItemText"},

//ListItem的XML文件里面的两个TextViewID

newint[]{R.id.ItemTitle,R.id.ItemText});

//添加并且显示

list.setAdapter(mSchedule);

}

另一个例子:

main.xml的源代码,跟上一个一样,这里就不作解释了,直接贴出my_imageitem.xml的代码,就是它实现ImageItem的UI:

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

>

        android:

id="@+id/RelativeLayout01"

        android:

layout_width="fill_parent"

        xmlns:

android="

        android:

layout_height="wrap_content"

        android:

paddingBottom="4dip"

        android:

paddingLeft="12dip">

        

              android:

layout_width="wrap_content"

              android:

layout_height="wrap_content"

              android:

id="@+id/ItemImage">

        

        

              android:

text="TextView01"

              android:

layout_height="wrap_content"

              android:

textSize="

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1