玩转Android---UI篇---布局

原址:http://hualang.iteye.com/category/143855

LinearLayout(线性布局)

LinearLayout是在线性方向显示View元素的一个ViewGroup,可以是水平方向,也可以是垂直方向

你可以重复使用LinearLayout,如果你想使用嵌套多层的LinearLayout的话,你可以考虑使用RelativeLayout来替换.

1、开始创建一个工程名字叫做HelloLinearLayout

 

2、打开res/layout/main.xml文件并且插入如下内容

Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <LinearLayout  
  8.         android:orientation="horizontal"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent"  
  11.         android:layout_weight="1">  
  12.         <TextView  
  13.             android:text="red"  
  14.             android:gravity="center_horizontal"  
  15.             android:background="#aa0000"  
  16.             android:layout_width="wrap_content"  
  17.             android:layout_height="fill_parent"  
  18.             android:layout_weight="1"  
  19.         />  
  20.         <TextView  
  21.             android:text="green"  
  22.             android:gravity="center_horizontal"  
  23.             android:background="#00aa00"  
  24.             android:layout_width="wrap_content"  
  25.             android:layout_height="fill_parent"  
  26.             android:layout_weight="1"  
  27.         />  
  28.         <TextView  
  29.             android:text="blue"  
  30.             android:gravity="center_horizontal"  
  31.             android:background="#0000aa"  
  32.             android:layout_width="wrap_content"  
  33.             android:layout_height="fill_parent"  
  34.             android:layout_weight="1"  
  35.         />  
  36.         <TextView  
  37.             android:text="yellow"  
  38.             android:gravity="center_horizontal"  
  39.             android:background="#aaaa00"  
  40.             android:layout_width="wrap_content"  
  41.             android:layout_height="fill_parent"  
  42.             android:layout_weight="1"  
  43.         />  
  44.     </LinearLayout>  
  45.     <LinearLayout  
  46.         android:orientation="vertical"  
  47.         android:layout_width="fill_parent"  
  48.         android:layout_height="fill_parent"  
  49.         android:layout_weight="1">  
  50.         <TextView  
  51.             android:text="row one"  
  52.             android:textSize="15pt"  
  53.             android:layout_width="fill_parent"  
  54.             android:layout_height="wrap_content"  
  55.             android:layout_weight="1"  
  56.         />  
  57.         <TextView  
  58.             android:text="row two"  
  59.             android:textSize="15pt"  
  60.             android:layout_width="fill_parent"  
  61.             android:layout_height="wrap_content"  
  62.             android:layout_weight="1"  
  63.         />  
  64.         <TextView  
  65.             android:text="row three"  
  66.             android:textSize="15pt"  
  67.             android:layout_width="fill_parent"  
  68.             android:layout_height="wrap_content"  
  69.             android:layout_weight="1"  
  70.         />  
  71.         <TextView  
  72.             android:text="row four"  
  73.             android:textSize="15pt"  
  74.             android:layout_width="fill_parent"  
  75.             android:layout_height="wrap_content"  
  76.             android:layout_weight="1"  
  77.         />  
  78.     </LinearLayout>  
  79. </LinearLayout>  
 仔细检查这个XML文件。有一个根元素LinearLayout定义了它的方向是垂直的,所有的子View(一共有2个)都是被垂直方向堆起的,第一个子孩子是另一个以水平方向布局的LinearLayout,并且第二个子孩子是一个用垂直方向布局的LinearLayout,这些每一个被嵌套的LinearLayout都包含几个TextView元素,它们的方向是由父LinearLayout标签所定义。

 

3、现在打开HelloLinearLayout.java并且确定它已经在onCreate()方法中加载了res/layout/main.xml布局文件

Java代码   收藏代码
  1. public void onCreate(Bundle savedInstanceState) {  
  2.     super.onCreate(savedInstanceState);  
  3.     setContentView(R.layout.main);  
 

setContentView(int)方法为Activity加载了布局文件,由资源resource ID所指定---R.layout.main指的是res/layout/main.xml布局文件

 

4、运行程序,你可以看到如下的情况


RelativeLayout(相对布局)


RelativeLayout是一个在相对位置上显示子View元素的VeiwGroup,一个视图的位置,可以指定为相对于兄妹的元素(比如一个给定的与孙的左边或者下边)或者心爱那个对于RelativeLayout区域的位置(比如与底部对齐,剩下的中心)

 

一个RelativeLayout是一个非常强大使用的为设置用户界面的布局,因为它可以消除嵌套的视图组ViewGroup,如过你发现你用了几个嵌套的LinearLayout组,你可以替换为一个单独的RelativeLayout

 

1、开始一个新的工程,名字叫做HelloRelativeLayout

 

2、打开res/layout/main.xml文件并且插入如下信息

 

Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <TextView    
  8.         android:id="@+id/label"  
  9.         android:layout_width="fill_parent"   
  10.         android:layout_height="wrap_content"   
  11.         android:text="Type here:"  
  12.     />  
  13.     <EditText  
  14.         android:id="@+id/entry"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:background="@android:drawable/editbox_background"  
  18.         android:layout_below="@id/label"  
  19.     />  
  20.     <Button  
  21.         android:id="@+id/ok"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content"  
  24.         android:layout_below="@id/entry"  
  25.         android:layout_alignParentRight="true"  
  26.         android:layout_marginLeft="10dip"  
  27.         android:text="OK"  
  28.     />  
  29.     <Button  
  30.         android:layout_width="wrap_content"  
  31.         android:layout_height="wrap_content"  
  32.         android:layout_toLeftOf="@id/ok"  
  33.         android:layout_alignTop="@id/ok"  
  34.         android:text="Cancel"  
  35.     />  
  36. </RelativeLayout>  
 3、注意到每一个android:layout_*属性,比如layout_below,layout_alignParentRightRight,和layout_toLeftOf,当用一个RelativeLayout的时候,你可以用这些属性来描述你想要的每个视图View的位置,每一个这些属性都定义一个不懂种类的相对位置,一些属性用到同级视图的资源ID来定义自己的相对位置。比如最后一个Button是被定义到位于被定义ID为ok的视图的左边和顶部对齐,

所有的layout布局属性都被定义在RelativeLayout.LayoutParams中


4、 现在打开HelloLinearLayout.java并且确定它已经在onCreate()方法中加载了res/layout/main.xml布局文件
Java代码   收藏代码
  1. public void onCreate(Bundle savedInstanceState) {  
  2.        super.onCreate(savedInstanceState);  
  3.        setContentView(R.layout.main);  
 
5、你可以看到如下的布局


TableLayout(表格布局)

TableLayout是一个以行、列显示视图View的视图组

 

1、开始一个新的工程,名字叫做HelloTableLayout

 

2、打开res/layout/main.xml文件并且插入如下内容

Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TableLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:stretchColumns="1">  
  7.     <TableRow>  
  8.         <TextView  
  9.             android:layout_column="1"  
  10.             android:text="Open..."  
  11.             android:padding="3dip"  
  12.         />  
  13.         <TextView  
  14.             android:text="Ctrl-O"  
  15.             android:gravity="right"  
  16.             android:padding="3dip"  
  17.         />  
  18.     </TableRow>  
  19.     <TableRow>  
  20.         <TextView  
  21.             android:layout_column="1"  
  22.             android:text="Save..."  
  23.             android:padding="3dip"  
  24.         />  
  25.         <TextView  
  26.             android:text="Ctrl-S"  
  27.             android:gravity="right"  
  28.             android:padding="3dip"  
  29.         />  
  30.     </TableRow>  
  31.     <TableRow>  
  32.         <TextView  
  33.             android:layout_column="1"  
  34.             android:text="Save as..."  
  35.             android:padding="3dip"  
  36.         />  
  37.         <TextView  
  38.             android:text="Ctrl-Shift-S"  
  39.             android:gravity="right"  
  40.             android:padding="3dip"  
  41.         />  
  42.     </TableRow>  
  43.     <View  
  44.         android:layout_height="2dip"  
  45.         android:background="#FF909090"  
  46.     />  
  47.     <TableRow>  
  48.         <TextView  
  49.             android:text="X"  
  50.             android:padding="3dip"  
  51.         />  
  52.         <TextView  
  53.             android:text="Import..."  
  54.             android:padding="3dip"  
  55.         />  
  56.     </TableRow>  
  57.     <TableRow>  
  58.         <TextView  
  59.             android:text="X"  
  60.             android:padding="3dip"  
  61.         />  
  62.         <TextView  
  63.             android:text="Export..."  
  64.             android:padding="3dip"  
  65.         />  
  66.         <TextView  
  67.             android:text="Ctrl-E"  
  68.             android:gravity="right"  
  69.             android:padding="3dip"  
  70.         />  
  71.     </TableRow>  
  72.     <View  
  73.         android:layout_height="2dip"  
  74.         android:background="#FF909090"  
  75.     />  
  76.     <TableRow>  
  77.         <TextView  
  78.             android:layout_column="1"  
  79.             android:text="Quit"  
  80.             android:padding="3dip"  
  81.         />  
  82.     </TableRow>  
  83. </TableLayout>  

 

注意到这个文件类似于HTML的table的结构,TableLayout元素就像是HTML中的<table>元素;TableRow就像是一一个<tr>元素;但是对于每一个单元格,你可以用各种视图元素,在这里例子里,每个单元格用TextView,在这些行之间,还有一个基本View,用来画水平线

TextView中的一些属性
android:layout_column="1":表示控件放在标号为1的列上,标号是从0开始的
android:gravity="right":定义字体在父控件中显示在右边
android:stretchColumns="1":设置自动拉伸哪些列,列ID从0开始,多个列的话用","分隔。这里的作用是让第2列可以扩展到所有可用空间
android:shrinkColumns:设置自动收缩哪些列,列ID从0开始,多个列的话用","分隔
android:collapseColumns:设置自动隐藏哪些列,列ID从0开始,多个列的话用","分隔

顺便:android:layout_span表示一个控件占几列空间

下面的是基本的View,是在屏幕上画一条2dip高的一条横线
 <View
  android:layout_height="2dip"
  android:background="#FF909090"
 />

 

3、运行结果如下:



Tab Layout(选项卡布局)

为了创建一个选项卡的UI,你需要使用一个TabHost和一个TabWidget,TabHost必须是布局文件的根节点,它包含了为了显示选项卡的TabWidget和一个用于显示选项内容的FrameLayout

你可以用一或两种方法实现你的选项卡内容:在用一个Activity中用选项卡来在视图之间切换,或者用用选项卡来改变所有的分离的Activity。你根据你的需求来使用你想在程序中的方法,但是如果每个选项卡提供一个独特的用户Activity,那么为每个选项卡实现独立的Activity是有意义的,所有你最好在你的离散群里管理应用程序,要好过使用大量的应用程序和布局文件。

在这个例子中,你可以创建一个为每个单独的Activity创建选项卡来创建一个选项卡UI

 

1、开始一个新的工程,叫做HelloTabWidget

 

2、第一,创建三个独立的Activity程序在你的工程中:ArtistsActivity,AlbumsActivity,和SongsActivity,他们每个代表一个单独的选项卡,现在用TextView来没每个程序显示一个简单的信息,比如:

Java代码   收藏代码
  1. package org.hualang.tabwidget;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.TextView;  
  6.   
  7. public class AlbumsActivity extends Activity {  
  8.     public void onCreate(Bundle savedInstanceState)  
  9.     {  
  10.         super.onCreate(savedInstanceState);  
  11.          TextView textview = new TextView(this);  
  12.          textview.setText("This is the Albums tab");          
  13.         setContentView(textview);  
  14.     }  
  15. }  

 

Java代码   收藏代码
  1. package org.hualang.tabwidget;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.TextView;  
  6.   
  7. public class SongsActivity extends Activity {  
  8.     public void onCreate(Bundle savedInstanceState)  
  9.     {  
  10.         super.onCreate(savedInstanceState);  
  11.          TextView textview = new TextView(this);  
  12.          textview.setText("This is the Songs tab");   
  13.         setContentView(textview);  
  14.     }  
  15.   
  16. }  

 

Java代码   收藏代码
  1. package org.hualang.tabwidget;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.TextView;  
  6.   
  7. public class ArtistsActivity extends Activity {  
  8.     /** Called when the activity is first created. */  
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.          TextView textview = new TextView(this);  
  13.          textview.setText("This is the Artists tab");   
  14.         setContentView(textview);  
  15.     }  
  16. }  

 注意这个例子中不需要布局文件,只需要创建一个TextView,并且为文本赋值即可。重复创建三个类似的Activity,并且要在AndroidManifest.xml文件中注册,否则报错

3、你需要为每个选项卡设置一个icon,每个icon,你可以有两个版本,一个是当选项卡被选中的时候,另一个是当选项卡未被选中的时候。一般设计来说,建议当被选中的时候用灰色,的那个未被选中的时候用白色,比如




 
 你可以拷贝这两张图片来做实验用
现在创建一个状态图片列表来制定每个选项卡不同状态的时候所指定的图片
 ①把图排尿保存在res/drawable/目录下
 ②在res/drawable/目录下创建一个名为ic_tab_artists.xml文件,并且插入如下信息

Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3. <!-- When selected,use grey -->  
  4.     <item android:drawable="@drawable/ic_tab_artists_grey"  
  5.         android:state_selected="true"/>  
  6.     <!-- When not selected ,use white -->  
  7.     <item android:drawable="@drawable/ic_tab_artists_white"/>  
  8. </selector>  

 

上面这个文件,当选项卡的状态改变的时候,选项卡就会自动的在两种已经定义的图片之间切换

 

4、打开res/layout/main.xml文件并且插入如下信息

Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@android:id/tabhost"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.     <LinearLayout  
  7.         android:orientation="vertical"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         android:padding="5dp">  
  11.         <TabWidget  
  12.             android:id="@android:id/tabs"  
  13.             android:layout_width="fill_parent"  
  14.             android:layout_height="wrap_content" />  
  15.         <FrameLayout  
  16.             android:id="@android:id/tabcontent"  
  17.             android:layout_width="fill_parent"  
  18.             android:layout_height="fill_parent"  
  19.             android:padding="5dp" />  
  20.     </LinearLayout>  
  21. </TabHost>  

 

这个布局文件将显示选项卡兵器提供每个Activity之间的导航
TabHost要求一个TabWidget和一个FrameLayout布局,为了使TabWidget和FrameLayout的位置处于垂直方向,需要一个LinearLayout,FrameLayout是每个选项卡内容的地方,之所以那里的内容是空的是因为在TahHost中将自动为每个Activity嵌入

注意,TabWidget和FrameLayout元素的ID标签和tabcontent元素,这些名称必须使用,因为TahHost检索他们的引用,它恰好期望的是这些名字

 

6、编写HelloTabWidget。继承TabWidget

Java代码   收藏代码
  1. package org.hualang.tabwidget;  
  2.   
  3. import android.app.TabActivity;  
  4. import android.content.Intent;  
  5. import android.content.res.Resources;  
  6. import android.os.Bundle;  
  7. import android.widget.TabHost;  
  8.   
  9. public class HelloTabWidget extends TabActivity  {  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.main);  
  13.   
  14.         Resources res = getResources(); // Resource object to get Drawables  
  15.         TabHost tabHost = getTabHost();  // The activity TabHost  
  16.         TabHost.TabSpec spec;  // Resusable TabSpec for each tab  
  17.         Intent intent;  // Reusable Intent for each tab  
  18.   
  19.         // Create an Intent to launch an Activity for the tab (to be reused)  
  20.         intent = new Intent().setClass(this, ArtistsActivity.class);  
  21.   
  22.         // Initialize a TabSpec for each tab and add it to the TabHost  
  23.         spec = tabHost.newTabSpec("artists").setIndicator("Artists",  
  24.                           res.getDrawable(R.drawable.ic_tab_drawable))  
  25.                       .setContent(intent);  
  26.         tabHost.addTab(spec);  
  27.   
  28.         // Do the same for the other tabs  
  29.         intent = new Intent().setClass(this, AlbumsActivity.class);  
  30.         spec = tabHost.newTabSpec("albums").setIndicator("Albums",  
  31.                           res.getDrawable(R.drawable.ic_tab_drawable))  
  32.                       .setContent(intent);  
  33.         tabHost.addTab(spec);  
  34.   
  35.         intent = new Intent().setClass(this, SongsActivity.class);  
  36.         spec = tabHost.newTabSpec("songs").setIndicator("Songs",  
  37.                           res.getDrawable(R.drawable.ic_tab_drawable))  
  38.                       .setContent(intent);  
  39.         tabHost.addTab(spec);  
  40.   
  41.         tabHost.setCurrentTab(2);  
  42.     }  
  43.   
  44.   
  45. }  

 

运行结果:














评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值