layout程序说明:
在主页面Activity:
设置了Button,OnClickListener(onClick方法);
给Button设置单击监听器
在每个Button的单击监听中,跳转到另一个activity
LinearLayout标签:将自己的子元素按照一个方向排列(从上到下,从左到右)
android:orientation="vertical" "horizontal"
FrameLayout标签:预定好的空白区域,可以往里填充元素。但是所有元素都会放在其左上区域,会重叠显示
android:layout_width="wrap_content" //fill_parent
android:layout_height="wrap_content" //fill_parent
android:id="@+id/photo" //id用于在程序中使用。findViewById(R.id.photo)
android:src="@drawable/bg"
RelativeLayout标签:子元素是相对于子元素相对布局的
android:background="@drawable/blue" //@drawable/blue是#770000ff
android:padding="10dip" //依赖于设备的像素。 padding:填充;margin:边距
android:layout_below="@id/label" //经典的相对布局
android:layout_alignParentRight="true" //和父容器的右边齐平
android:layout_marginLeft="10dip" //左边距为10dip
android:layout_toLeftOf="@id/cancel" //在XXX左边
android:layout_alignTop="@id/cancel" //和xxx高度齐平
复杂应用:在程序里将两个RelativeLayout组成一个LinearLayout,然后把这个Layout赋给Activity。
(1)获取RelativeLayout
LayoutInflater:对xml布局文件解析,生成RelativeLayout
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layoutLeft = (RelativeLayout) inflate.inflate(R.layout.left, null);
RelativeLayout layoutRight = (RelativeLayout) inflate.inflate(R.layout.right, null);
(2)RelativeLayout.LayoutParams 关于layout的长,宽等信息
RelativeLayout.LayoutParams relParam = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
TableLayout标签:表格布局
TableRow标签:定义了表格布局里的一行。每一行里面都可以自由加入一些组件
OnClickListener listerner0=null;
Button button0;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
listerner0=new OnClickListener(){
public void onClick(){
Intent intent0=new Intent(ActivityMain.this,ActivityFrameLayout.class);
setTitle("XXX");
startActivity();
}
};
setContextView(R.layout.main);
button0=(Button)findViewByid(R.id.button0);
button0.setOnClickListener(listerner0);
}
//复杂应用
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layoutMain = new LinearLayout(this);
layoutMain.setOrientation(LinearLayout.HORIZONTAL);
setContentView(layoutMain);
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layoutLeft = (RelativeLayout) inflate.inflate(
R.layout.left, null);
RelativeLayout layoutRight = (RelativeLayout) inflate.inflate(
R.layout.right, null);
RelativeLayout.LayoutParams relParam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutMain.addView(layoutLeft, 100, 100);
layoutMain.addView(layoutRight, relParam);
}