Android资源的使用之Layout

[ 2011-12-25 17:22:23 | 作者: admin ]
字号: | |
在Android应用中一个屏幕的视图通常是通过加载自一个XML资源文件,这个XML文件就是布局资源文件。一个布局资源也是通过ID号在R.java中标识,代码中使用布局资源如下:
@Overridepublic void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
             // Set the layout for this activity. You can find it
         // in res/layout/hello_activity.xml
         setContentView(R.layout.hello_activity);
   }

setContentView(R.layout.hello_activity);指出Activity的视图来自/res/layout/hello_activity.xml的布局资源文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" >
<TextView android:id="@+id/text1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
<Button android:id="@+id/b1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@+string/hello" />
</LinearLayout>


这里用到了线性页面布局LinearLayout,这是Android中最常用的布局,表示布局中的组件按一个一行的显示出来。

常用的布局还有FrameLayout(框架布局)、TableLayout(表格布局)、AbsoluteLayout(绝对位置布局)、RelativeLayout(相对位置布局)。

FrameLayout、LinearLayout、AbsoluteLayout、RelativeLayout均是android.view.ViewGroup的子类。

TableLayout则是LinearLayout的子类,用表格来排列组件,如果TableLayout中的组件没有放入TableRow的话,那么就会按LinearLayout显示。

AbsoluteLayout则根据设定好的坐标定位显示,AbsoluteLayout中两个重要属性是:android:layout_x组件在屏幕中的X坐标,android:layout_y组件在屏幕中的Y坐标。

RelativeLayout是按照相对于某个组件的位置来进行布局的,也就是说参考某个组件,置于此组件的上、下、左、右。

FrameLayout则是一个比较特殊的布局,是多个组件靠左上角叠加放置。


来源:http://www.cnblogs.com/dawei/archive/2010/04/27/1722424.html
[最后修改由 admin, 于 2011-12-25 17:25:07]
评论Feed 评论Feed: http://blog.xg98.com/feed.asp?q=comment&id=1776

这篇日志没有评论。

此日志不可发表评论。