Android API解析

1.1 组件的xml Layout

注意上例中hello andriod中,布局通过源码来实现。【如果控件多,而且修改维护将很麻烦】

好在Android提供了一种可选的UI 构建模型【XMl-based layout files.

比如:

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

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

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:text="Hello,通过XML-based layout"/>

这种类似树形结构的元素,可以很方便的构建UI.

对应元素的含义如下:

属性

含义

xmlns:android

所有布局必须具有该属性,基础属性

android:layout_width

定义当前视图在屏幕上可以消费的宽度,fill_parent即填充整个屏幕。

android:layout_height

定义可消费的高度

android:text

设置文本视图TextView所办好的内容

这些xml布局文件在那里存放呢?

工程的res目录下,用来存放none-code assets 程序所需要的。【图片、定位字符、xml 布局文件】

Eclipse 插件已经默认创建了这些文件,如图所示

<shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 167.25pt; HEIGHT: 143.25pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:/DOCUME~1/hu/LOCALS~1/Temp/msohtml1/01/clip_image001.png"></imagedata></shape>

res/layout/main.xml文件进行编辑,修改内容如下:

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

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

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:text="Hello,通过XML-based layout"/>

输入效果:

<shape id="_x0000_i1026" style="WIDTH: 414.75pt; HEIGHT: 205.5pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:/DOCUME~1/hu/LOCALS~1/Temp/msohtml1/01/clip_image003.png"><font size="3"></font></imagedata></shape>

1.2 R.java 分析

在源码路径中,跟Acttivity 相同目录下,有R.java

<shape id="_x0000_i1027" style="WIDTH: 200.25pt; HEIGHT: 89.25pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:/DOCUME~1/hu/LOCALS~1/Temp/msohtml1/01/clip_image005.png"><font face="Calibri" size="3"></font></imagedata></shape>

该文件是一个索引,定义在文件中的所有资源。在源码中利用该类作为一个简单的引用来指向你的资源【包含在工程中的】。可以方便的让你快速定位。

实例代码如下:

package org.huareal.android.hello;

public final class R {

public static final class attr {

}

public static final class drawable {

public static final int icon=0x7f020000;

}

public static final class layout {

public static final int back_main=0x7f030000;

public static final int main=0x7f030001;

}

public static final class string {

public static final int app_name=0x7f040000;

}

}

注意:该内部类名称为layout, 以及成员main. 针对Eclipse 插件来说,请注意你添加了一个xml布局文件,就会自动重新生成R.java文件。

当你改变东西时,不要拷贝,粘贴处理。而是通过代码编译属性来处理。

实例代码:

public class HelloAndriod extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

android.widget.TextView tv=new TextView(this);

tv.setText("你好,我的Android ^_^");

this.setContentView(tv);

//setContentView(R.layout.main);

}

}

<shape id="_x0000_i1028" style="WIDTH: 415.5pt; HEIGHT: 56.25pt" type="#_x0000_t75"><imagedata o:title="" src="file:///C:/DOCUME~1/hu/LOCALS~1/Temp/msohtml1/01/clip_image007.png"></imagedata></shape>

如果采用标准的

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

/**

android.widget.TextView tv=new TextView(this);

tv.setText("你好,我的Android ^_^");

this.setContentView(tv);

*/

setContentView(R.layout.main);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值