@stepbystep
2015-01-28T05:37:21.000000Z
字数 22891
阅读 14579
Android
说明: 本节主要讲解了Android的UI框架View 和ViewGroup等, 同时讲解一些常用的布局,如LinearLayout,RelativeLayout等
课前复习
控件点击事件注册的4中方法:
// Res/layout/xxx.xml
// <Button
// onClick="click"/>
// 在Activity中写一个如下的方法: 函数名与之对应
Public void click(View view){
}
// button 是查找后赋值的控件
button.setOnClickListener(new OnClickListener(){
// 重载函数
Public void onClick(View view){
}
});
// 在Activity类中再自定义一个MyOnClickListener类
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//---其他代码
// button 是查找后赋值的控件
button.setOnClickListener(new MyOnClickListener();
}
// 自定义内部类
Private final class MyOnClickListener implements OnClickListener{
Public void onClick(View view){
}
}
}
Public class MainActivity extends Activity implements OnClickListener{
@Override
Public void onClick(View view){
///
}
}
Android的UI 开发使用嵌套结构,在ViewGroup中嵌套ViewGroup,每一层有任意数目的View。如下图:
需要注意的是嵌套次数最好不要超过10层,否则会降低效率,上图是3层
View和ViewGroup的结构图
深红色<-蓝色<-粉色<-青色<-土黄色
在layout目录下新建一个xml文件,按照布局格式布局。
xml中每个元素的是View或者ViewGroup的子孙类的对象。他就像一颗树一样,根节点只有一个ViewGroup,叶节点都是View对象,分支节点都是ViewGroup
xxpx: 占用xx个像素,不利于屏幕适配,一般不用
android:laout_width
和android:layout_height
属性表示该控件的宽度和高度,可取如下值:
wrap_content 控件占用自身大小的空间
match_content 控件占满其父空间,早期版本叫做fill_content
android:layout_margin**(Left, Right, Top, Bottom)
设置控件距离其他控件左右上下的间距。android:padding_**(Left, Right, Top, Bottom)
设置控件内容距离空间的左右上下边界的间距。android:layout_gravity
属性用于设置控件在父控件中的对其属性,如left(左对齐)right(右对齐)center(居中)等android:gravity
是设置当前控件的内容在当前控件内部的对其属性,可取值与layout_gravity一样。android:id
为控件指定相应的ID android:text
指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串 android:textSize
指定控件当中字体的大小 android:background
指定该控件所使用的背景色,RGB命名法 android:width
指定控件的宽度 android:height
指定控件的高度 首先新建LayoutDemo工程,并新建MainActivity和LayoutActivity。
工程文件结构最后大致如下图:
其中MainActivity为主界面,主要代码如下:
package com.example.shiyanlou.layoutdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册点击事件
findViewById(R.id.btn_linear).setOnClickListener(this);
findViewById(R.id.btn_relative).setOnClickListener(this);
findViewById(R.id.btn_table).setOnClickListener(this);
findViewById(R.id.btn_frame).setOnClickListener(this);
findViewById(R.id.btn_absolute).setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(this, LayoutActivity.class);
String layout_id = "layout_id";
/**
* 此处因为只需要展示每个布局文件,用不着新建多个Activity,只需要在LayoutActivity中的onCreate函数导航
* 布局文件的时候通过intent.getIntent().getIntExtra("layout_id")的方法获取布局文件对应的id号。
* 即可设置不同的布局文件
*/
switch (v.getId()){
case R.id.btn_linear:
intent.putExtra(layout_id, R.layout.linear_layout);
break;
case R.id.btn_relative:
intent.putExtra(layout_id, R.layout.relative_layout);
break;
case R.id.btn_table:
intent.putExtra(layout_id, R.layout.table_layout);
break;
case R.id.btn_frame:
intent.putExtra(layout_id, R.layout.frame_layout);
break;
case R.id.btn_absolute:
intent.putExtra(layout_id, R.layout.absolute_layout);
break;
}
startActivity(intent);
}
}
MainActivity的布局文件res\layout\activity_main.xml如下
<!--这里用到了线性布局, 利用layout_weight="1"使得每个按钮均匀显示在手机中-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="20dp"
>
<!--应用了style属性,因为这5个按钮除了显示的内容和id号不一样以外,其余属性基本上一样
此时就可以利用style属性,在res/values/styles.xml文件中添加一个style标签,加上属性内容
再在控件的属性中用style="@style/**"的方式,引用style
这样做的好处是当需要改变属性的时候,只要改变style文件中的内容,布局结果就会发生变化-->
<Button
android:id="@+id/btn_linear"
style="@style/blue_btn_style"
android:text="LinearLayout"
/>
<Button
android:id="@+id/btn_relative"
style="@style/blue_btn_style"
android:text="RelativeLayout"
/>
<Button
android:id="@+id/btn_table"
style="@style/blue_btn_style"
android:text="TableLayout"
/>
<Button
android:id="@+id/btn_frame"
style="@style/blue_btn_style"
android:text="FrameLayout"
/>
<Button
android:id="@+id/btn_absolute"
style="@style/blue_btn_style"
android:text="AbsoluteLayout"
/>
</LinearLayout>
其中style="@style/blue_btn_style"
引用的style文件内容如下,设置了Button的样式
res\values\styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light">
<!-- Customize your theme here. -->
</style>
<!--蓝色按钮的背景style,将各种属性集一身,减少布局文件中重复的内容-->
<style name="blue_btn_style">
<item name="android:textSize">18sp</item>
<item name="android:textColor">#fff</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_width">match_parent</item>
<!--设置了background为@drawable/selector_btn_blue
表示背景对应这样的一个drawable文件,实现了背景选择器-->
<item name="android:background">@drawable/selector_btn_blue</item>
<item name="android:layout_marginLeft">20dp</item>
<item name="android:layout_marginRight">20dp</item>
<item name="android:layout_marginTop">20dp</item>
<item name="android:layout_weight">1</item>
</style>
</resources>
背景选择器的内容如下 res\drawable\selector_btn_blue.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--selector 背景选择器, 每一个item对应一种状态,从上至下扫描,如果满足则选择该item作为背景
主要用来处理按下的效果,获取获取焦掉与离开焦点的效果
正常情况下 state_pressed="false" 且 state_focused="false",此时应选择最后一个item(此item无特殊要求)
当控件被按下或者获取焦点的时候,就会匹配到第一个item,颜色就会变深,从而得到了按下的效果。
当然每个item可以有其他的属性,这里的radius就是圆角的意思。
-->
<item android:state_pressed="true"><shape>
<corners android:radius="5dp"/>
<solid android:color="#2988e4"></solid>
</shape></item>
<item android:state_focused="true"><shape>
<corners android:radius="5dp"/>
<solid android:color="#2988e4"></solid>
</shape></item>
<item><shape>
<corners android:radius="5dp"/>
<solid android:color="#4AA9EE"></solid>
</shape></item>
</selector>
进入界面效果如下:
点击按钮的按下效果如下: 可以看到颜色明显加深了
LayoutActivity为点击按钮进入的布局界面
package com.example.shiyanlou.layoutdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class LayoutActivity extends Activity {
private int layout_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 根据Intent传递的数据,得到导航的布局id号。
layout_id = getIntent().getIntExtra("layout_id", R.layout.linear_layout);
// 设置全屏,需要在setContentView之前调用
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 将layout_id设置为当前布局文件的id号
setContentView(layout_id);
}
}
线性布局知识点
match_parent
,则layout_weight值越大,所占比例越小。 wrap_content
则 layout_weight值越大,所占比例越小。线性布局实验
底部按钮按下效果如下: 可以看到颜色变成了深红色
res\layout\linear_layout.xml文件主要内容如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<!--内层再使用两个线性布局,分别表示上层的水平方式,和下半部分的垂直方式-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:padding="10dp"
android:orientation="horizontal">
<!--wrap_content和layout_weight一起使用时,控件所占比例跟layout_weight的值成正比-->
<!--android:text="第\n一\n列" \n在这里用于换行显示内容-->
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#FFDAB9"
android:gravity="center_horizontal"
android:textColor="#2a5caa"
android:text="第\n一\n列"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#FF82AB"
android:gravity="center_vertical|right"
android:textColor="#2a5caa"
android:text="第\n二\n列"
android:layout_weight="2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#EEEE00"
android:gravity="bottom|center_horizontal"
android:textColor="#2a5caa"
android:text="第\n三\n列"
android:layout_weight="3"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#ADFF2F"
android:gravity="center"
android:textColor="#2a5caa"
android:text="第\n四\n列"
android:layout_weight="4"/>
</LinearLayout>
<!--下面的所有Button使用了android:layout_weight="1"属性,
使得可以在下半部分均匀显示-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:orientation="vertical">
<!--使用了背景选择器selector_btn_red-->
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#fff"
android:background="@drawable/selector_btn_red"
android:text="第一行"/>
<Button
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#fff"
android:background="@drawable/selector_btn_red"
android:text="第二行"/>
<Button
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#fff"
android:background="@drawable/selector_btn_red"
android:text="第三行"/>
</LinearLayout>
</LinearLayout>
这里又用到了背景选择器,直接在控件的backgroud设置,注意,引用背景选择器,需要用@drawable方式。红色背景选择器如下,几乎同蓝色selector_btn_blue.xml一样。只不过换了颜色
res/drawable/selector_btn_red.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><shape>
<corners android:radius="5dp"/>
<solid android:color="#FF1120"></solid>
</shape></item>
<item android:state_focused="true"><shape>
<corners android:radius="5dp"/>
<solid android:color="#FF1120"></solid>
</shape></item>
<item><shape>
<corners android:radius="5dp"/>
<solid android:color="#FF6B6B"></solid>
</shape></item>
</selector>
相对布局知识点
android:layout_above
将该控件的底部至于给定ID控件之上 android:layout_below
将该控件的顶部至于给定ID的控件之下 android:layout_toLeftOf
将该控件的右边缘和给定ID的控件左边缘对齐 android:layout_toRightOf
将该控件的左边缘和给定ID的控件的右边缘对齐
android:layout_alignBottom
将该控件的底部边缘与给定ID控件的底部边缘对齐
android:layout_alignLeft
将该控件的左边缘与给定ID控件的左边缘对齐 android:layout_alignRight
将该控件的右边缘与给定ID控件的右边缘对齐 android:layout_alignTop
将该控件的顶部边缘与给定ID控件的顶部对齐
android:alignParentBottom
如果该值为true,则将该控件的底部和父控件的底部对齐
android:layout_alignParentLeft
如果该值为true,则将该控件左边与父控件的左边对齐 android:layout_alignParentRight
如果该值为true,则将该控件的右边与父控件的右边对齐 android:layout_alignParentTop
如果该值为true,则将该控件的顶部与父控件的顶部对齐
android:layout_centerHorizontal
如果为真,该控件将被至于水平方向的中央
android:layout_centerInParent
如果为真, 该控件将被至于父控件水平方向和垂直方向的中央 android:layout_centerVertical
如果为真,该控件将被至于垂直方向的中央 相对布局四眼
本实验中相对布局以英雄联盟的英雄介绍为目的,制作了一个简单的布局界面。
用到了如下的图片资源,可以点击右键另存为后作为实验。
布局效果如下:
布局文件res\layout\relative_layout.xml文件如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FAEBD7"
>
<!--内层相对布局,默认暂居屏幕顶端-->
<RelativeLayout
android:id="@+id/top_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ACE383">
<ImageView
android:id="@+id/txt_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/manwang"
/>
<!--该控件以左边的ImageView为标识
android:layout_toRightOf="@id/txt_img"表示居其右-->
<TextView
android:id="@+id/txt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="蛮族之王"
android:textColor="#436EEE"
android:textSize="22sp"
android:layout_toRightOf="@id/txt_img"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"/>
<!--该控件以上边的TextView为标识
android:layout_below="@id/txt_name"表示位其下
android:layout_alignLeft="@id/txt_name"表示左边界相同-->
<LinearLayout
android:layout_below="@id/txt_name"
android:layout_alignLeft="@id/txt_name"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="近战"
android:padding="3dp"
android:textSize="12sp"
android:textColor="#fff"
android:background="#FF7A01"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上单"
android:padding="3dp"
android:textSize="12sp"
android:textColor="#fff"
android:layout_marginLeft="5dp"
android:background="#FF7A01"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打野"
android:padding="3dp"
android:textSize="12sp"
android:textColor="#fff"
android:layout_marginLeft="5dp"
android:background="#FF7A01"
/>
</LinearLayout>
<!--android:layout_alignLeft="@id/txt_name"
android:layout_alignBottom="@id/txt_img"
表示了该控件首先跟顶部的TextView沿着同一个左边界
同时也与左边的图片沿着同一个底边界-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#8B3A3A"
android:textSize="14sp"
android:layout_alignLeft="@id/txt_name"
android:layout_alignBottom="@id/txt_img"
android:layout_marginBottom="5dp"
android:text="蛮族之王,别名蛮子,泰达米尔,五秒男,蛮王。灵活性强,可以自由穿越地形切入战场,后期能力非常强."
/>
</RelativeLayout>
<!--android:layout_alignParentBottom="true"属性说明了该控件居于父控件的底部-->
<Button
android:id="@+id/btn_focus_vedio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#fff"
android:background="#B22222"
android:layout_alignParentBottom="true"
android:text="英雄视频"/>
<!--android:layout_above="@+id/btn_focus_vedio"属性说明了该控件居于Button的上方
注意,此处需要先布局Button,再根据其id布局下面的内容,尽管Button显示在最底部-->
<RelativeLayout
android:id="@+id/layout_q_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_focus_vedio"
android:padding="10dp">
<!--android:layout_centerVertical="true"
表示了该控件始终位于父控件的垂直中部-->
<ImageView
android:id="@+id/img_q"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:src="@drawable/manwang_q"
/>
<TextView
android:id="@+id/txt_q_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/img_q"
android:layout_marginLeft="10dp"
android:textColor="#9B30FF"
android:textSize="18sp"
android:text="嗜血杀戮【快捷键Q】"/>
<TextView
android:id="@+id/txt_q_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="泰达米尔对战斗极度饥渴,他受伤程度越高,攻击力越强。他能通过释放嗜血杀戮消耗怒气并治疗自己。"
android:layout_alignLeft="@+id/txt_q_name"
android:layout_below="@+id/txt_q_name"
android:textSize="12sp"
android:textColor="#595959"
android:layout_marginTop="5dp"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txt_q_name"
android:layout_below="@+id/txt_q_info"
android:layout_marginTop="15dp"
>
<TextView
android:id="@+id/txt_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="冷却"
android:textSize="12sp"
android:textColor="#FF7A01"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12/12/12/12/12"
android:textSize="12sp"
android:textColor="#595959"
android:layout_toRightOf="@+id/txt_first"
android:layout_alignTop="@+id/txt_first"
android:layout_marginLeft="10dp"
/>
<TextView
android:id="@+id/txt_second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="射程"
android:layout_below="@+id/txt_first"
android:layout_marginTop="10dp"
android:textSize="12sp"
android:textColor="#FF7A01"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="318"
android:textSize="12sp"
android:textColor="#595959"
android:layout_toRightOf="@+id/txt_second"
android:layout_alignTop="@+id/txt_second"
android:layout_marginLeft="10dp"
/>
<TextView
android:id="@+id/txt_third"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="效果"
android:layout_below="@+id/txt_second"
android:layout_marginTop="10dp"
android:textSize="12sp"
android:textColor="#FF7A01"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="被动:泰达米尔嗜血成性,获得5/10/15/20/25攻击力,每损失1%生命值额外增加0.15/0.2/0.25/0.3/0.35攻击力。泰达米尔消耗怒气,回复30/40/50/60/70(+0.3AP)。"
android:textSize="12sp"
android:textColor="#595959"
android:layout_toRightOf="@+id/txt_third"
android:layout_alignTop="@+id/txt_third"
android:layout_marginLeft="10dp"
/>
</RelativeLayout>
</RelativeLayout>
<!--此空间最后布局,
android:layout_below="@id/top_info"
android:layout_above="@id/layout_q_info"
两个属性使得该ImageView显示在两个id对应的控件之间-->
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:layout_below="@id/top_info"
android:layout_above="@id/layout_q_info"
android:src="@drawable/manwang_chuzhuang"/>
</RelativeLayout>
说明,本表格布局的内容主要参考了http://blog.sina.com.cn/s/blog_63c66eb60100u29p.html网址的内容,故在此说明。
表格布局知识点
Tablelayout简介
Tablelayout类以行和列的形式对控件进行管理,每一行为一个TableRow对象,或一个View控件。 当为TableRow对象时,可在TableRow下添加子控件,默认情况下,每个子控件占据一列。当为View时,该View将独占一行。
TableLayout行列数的确定
TableLayout的行数由开发人员直接指定,即有多少个TableRow对象(或View控件),就有多少行。 TableLayout的列数等于含有最多子控件的TableRow的列数。如第一TableRow含2个子控件,第二个TableRow含3个,第三个TableRow含4个,那么该TableLayout的列数为4.
TableLayout可设置的属性详解
TableLayout可设置的属性包括全局属性及单元格属性。
全局属性也即列属性,有以下3个参数:
android:stretchColumns
设置可伸展的列。该列可以向行方向伸展,最多可占据一整行。android:shrinkColumns
设置可收缩的列。当该列子控件的内容太多,已经挤满所在行,那么该子控件的内容将往列方向显示。android:collapseColumns
设置要隐藏的列。说明:列可以同时具备stretchColumns及shrinkColumns属性,若此,那么当该列的内容N多时,将“多行”显示其内容。(这里不是真正的多行,而是系统根据需要自动调节该行的layout_height)
单元格属性,有以下2个参数:
- android:layout_column
指定该单元格在第几列显示
- android:layout_span
指定该单元格占据的列数(未指定时,为1)
说明:一个控件也可以同时具备这两个特性。
表格布局实验内容
布局效果如图:
res/layout/table_layout.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"
android:padding="3dip"
>
<!-- 第1个TableLayout,用于描述表中的列属性。第0列可伸展,第1列可收缩,第2列被隐藏-->
<TextView
android:text="表1:全局设置:列属性设置"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="15sp"
android:background="#7f00ffff"/>
<TableLayout
android:id="@+id/table1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0"
android:shrinkColumns="1"
android:collapseColumns="2"
android:padding="3dip">
<TableRow>
<Button android:text="该列可伸展"/>
<Button android:text="该列可收缩"/>
<Button android:text="我被隐藏了"/>
</TableRow>
<TableRow>
<TextView android:text="我向行方向伸展,我可以很长 "/>
<TextView android:text="我向列方向收缩,我可以很深 "/>
</TableRow>
</TableLayout>
<!-- 第2个TableLayout,用于描述表中单元格的属性,包括:android:layout_column 及android:layout_span-->
<TextView
android:text="表2:单元格设置:指定单元格属性设置"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="15sp"
android:background="#7f00ffff"/>
<TableLayout
android:id="@+id/table2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="3dip">
<TableRow>
<Button android:text="第0列"/>
<Button android:text="第1列"/>
<Button android:text="第2列"/>
</TableRow>
<TableRow>
<TextView android:text="我被指定在第1列"
android:layout_column="1"/>
</TableRow>
<TableRow>
<TextView
android:text="我跨1到2列,不信你看!"
android:layout_column="1"
android:layout_span="2"
/>
</TableRow>
</TableLayout>
<!-- 第3个TableLayout,使用可伸展特性布局-->
<TextView
android:text="表3:应用一,非均匀布局"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="15sp"
android:background="#7f00ffff"/>
<TableLayout
android:id="@+id/table3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:padding="3dip"
>
<TableRow>
<Button android:text="一" ></Button>
<Button android:text="两字"></Button>
<Button android:text="三个字" ></Button>
</TableRow>
</TableLayout>
<!-- 第4个TableLayout,使用可伸展特性,并指定每个控件宽度一致,如1dip-->
<TextView
android:text="表4:应用二,均匀布局"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="15sp"
android:background="#7f00ffff"/>
<TableLayout
android:id="@+id/table4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:padding="3dip"
>
<TableRow>
<Button android:text="一" android:layout_width="1dip"></Button>
<Button android:text="两字" android:layout_width="1dip"></Button>
<Button android:text="三个字" android:layout_width="1dip"></Button>
</TableRow>
</TableLayout>
</LinearLayout>
效果如下:
res/layout/frame_layout.xml文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--FrameLayout中,常用于控件遮掩,比如看视频的时候,按暂停键,
弹出的控件就浮在播放内容的中间部位,此例中,控件在代码最后面的,展示在最前面-->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFE7BA"
android:text="第零层"
android:textSize="20sp"
android:gravity="center_horizontal"
/>
<TextView
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:background="#FFF68F"
android:text="第一层"
android:textSize="20sp"
android:gravity="center_horizontal"
/>
<!--可以看到第二层遮盖了第一层和第零层,却被第三层遮盖-->
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_vertical|right"
android:background="#FFBBFF"
android:text="第二层"
android:textSize="20sp"
android:gravity="center_horizontal"
/>
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#FF4040"
android:text="第三层"
android:textSize="20sp"
android:gravity="center_horizontal"
/>
</FrameLayout>
效果如下:
res/layout/absolute_layout.xml文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F0F8FF" >
<!--绝对布局基本上不会用,因为对于设备的兼容性不好,
两部分辨率不同的手机,展示结果可能差别很大-->
<!-- 定义一个文本框,使用绝对定位 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dip"
android:layout_y="20dip"
android:text="用户名:" />
<!-- 定义一个文本编辑框,使用绝对定位 -->
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="80dip"
android:layout_y="15dip"
android:width="200px" />
<!-- 定义一个文本框,使用绝对定位 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20dip"
android:layout_y="80dip"
android:text="密 码:" />
<!-- 定义一个文本编辑框,使用绝对定位 -->
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="80dip"
android:layout_y="75dip"
android:password="true"
android:width="200px" />
<!-- 定义一个按钮,使用绝对定位 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="130dip"
android:layout_y="135dip"
android:text="登 录" />
</AbsoluteLayout>