android学习总结2
1.LinearLayout线性布局
“LinearLayout”翻译成中文是“线性布局”,所谓线性布局就是在该标签下的所有子
元素会根据其orientation属性的值来决定是按行或者是按列逐个显示
其属性“xmlns:android”指定命名空间,顶级元素必须指定命名空间。而在该命名空
间中的控件的属性如layout_width,要在属性前加上“android:”做前缀。
其属性“layout_width”指定该元素的宽度,可选值有三种,“fill_parent”、
“wrap_content”、具体数字(单位为px)。其中“fill_parent”代表填满其父元素,对于
顶级元素来说,其父元素就是整个手机屏幕。“wrap_content”代表该元素的大小仅包裹其
自身内容,而数字则代表其占相应的px。
其属性“layout_height”指定该元素的高度,可选参数值与“layout_width”的参数意
《大话企业级Android开发》本教程官方讨论群:65882321
国士工作室电话:15711060468 Email: [email protected]
义相同。
其属性“orientation”指定子元素排列方式,其中指定为“vertical”则是子元素垂直
排列,每个子元素会占独立的一行,如上图,而另一个可选值为“horizontal”代表子元素
水平排列,即每个子元素会占独立的一列。示例main.xml布局文件如下。其对应的
strings.xml内容不变。
2.RelativeLayout(相对布局)
相对布局中的视图组件是按相互之间的相对位置来确定的,并不是线性布局中的必须
按行或按列单个显示
android:layout_below="@id/text":将该元素放到id为text的元素的下面
android:layout_toLeftOf="@id/ok" :放到id为ok的元素左边
android:layout_alignTop="@id/ok" :对齐id为ok的元素的顶部
注:布局之间可以相互嵌套使用,以完成更为复杂的布局效果
3.TableLayout表格布局
表格布局的风格跟HTML中的表格比较接近,只是所采用的标签不同。
<TableLayout>是顶级元素,说明采用的是表格布局
<TableRow>定义一个行
<TextView>定义一个单元格的内容
android:stretchColumns="0,1,2,3"
该属性指定每行都由第“0、1、2、3”列占满空白空间。
gravity指定文字对齐方式,本例都设为居中对齐。
padding指定视图与视图内容间的空隙,单位为像素。
4.FrameLayout帧布局
帧布局中的每一个组件都代表一个画面,默认以屏幕左上角作为(0,0)坐标,按组件
定义的先后顺序依次逐屏显示,后面出现的会覆盖前面的画面。用该布局可以实现动画效果。
主要内容为java代码 如下
package cn.class3g.activity;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
public class FrameLayoutTestChenActivityextends Activity {
FrameLayoutframe = null;
privateboolean flag = true;
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
findViews();
finalMyHandler myHandler = new MyHandler();
myHandler.sleep(10);
frame.setOnClickListener(newOnClickListener() {
publicvoid onClick(View v) {
flag= !flag;
myHandler.sleep(10);
}
});
}
privatevoid findViews() {
frame= (FrameLayout) this.findViewById(R.id.frame);
}
classMyHandler extends Handler {
inti = 0;
publicvoid handleMessage(Message msg) {
i++;
show(i% 8);
sleep(50);
}
publicvoid sleep(long delayMillis) {
if(flag) {
this.sendMessageDelayed(this.obtainMessage(10),delayMillis);
}
}
}
voidshow(int id) {
Drawable[]pic = new Drawable[10];
pic[0]= this.getResources().getDrawable(R.drawable.p1);
pic[1]= this.getResources().getDrawable(R.drawable.p2);
pic[2]= this.getResources().getDrawable(R.drawable.p3);
frame.setForeground(pic[id]);
}
}
今天实例
梅花按钮效果代码
<?xml version="1.0"encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/flower1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="100dp"
android:text="@string/flower1"/>
<Button
android:id="@+id/flower2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:text="@string/flower2"/>
<Button
android:id="@+id/flower3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:text="@string/flower3"/>
<Button
android:id="@+id/flower4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/flower4"/>
<Button
android:id="@+id/flower5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="200dp"
android:text="@string/flower5"/>
</RelativeLayout>