您的位置:首页 > 移动开发 > Android开发

android简单demo学习系例之排版(TableLayout)[code-based]

2009-02-06 20:56 706 查看
程序用到的图片


       bg1.jpg

   bg2.jpg

程序运行效果

 


    

 

对应的UI-TREE

 



 

 

相对布局的使用只有注意到控件ID就不难理解与编码

 

代码:

 

package com.shcolar.luo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
/**
* 简单的TableLayoutDemo 基于code-based
* @author shcolar.luo
*
*/
public class TableLayoutDemo extends Activity implements OnClickListener{

private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
//ViewGroup1-根节点 包含ViewGroup2-TableLayout 与 View-Button
RelativeLayout sRLayout = new RelativeLayout(this);
setContentView(sRLayout);
//ViewGroup2
TableLayout sTLayout = new TableLayout(this);
//设置ID,相对布局用到
sTLayout.setId(1);
sRLayout.addView(sTLayout, new TableLayout.LayoutParams(WC, WC));
//ViewGroup2的第一行
TableRow sRow1 = new TableRow(this);
sTLayout.addView(sRow1, new TableLayout.LayoutParams(WC, WC));

ImageView sImageView1 = new ImageView(this);
sImageView1.setImageDrawable(this.getResources().getDrawable(R.drawable.bg1));

EditText sEditText1 = new EditText(this);

sRow1.addView(sImageView1);
sRow1.addView(sEditText1);
//ViewGroup2的第二行
TableRow sRow2 = new TableRow(this);
sTLayout.addView(sRow2, new TableLayout.LayoutParams(WC, WC));

ImageView sImageView2 = new ImageView(this);
sImageView2.setImageDrawable(this.getResources().getDrawable(R.drawable.bg2));

EditText sEditText2 = new EditText(this);

sRow2.addView(sImageView2);
sRow2.addView(sEditText2);

Button sExitButton = new Button(this);
sExitButton.setText("退出");
sExitButton.setOnClickListener(this);

RelativeLayout.LayoutParams sLayoutParams =
new RelativeLayout.LayoutParams(WC, WC);
sLayoutParams.addRule(RelativeLayout.BELOW, 1);
sLayoutParams.topMargin = 20;
//sExitButton相对于ViewGroup2排版
sRLayout.addView(sExitButton, sLayoutParams);

}
/**
* 点击事件处理
*/
public void onClick(View v) {

this.finish();

}
}


 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android button class