实现一个登陆界面:
相对布局:
package cn.csdn.codeui;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class LoginRelativeActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initUI();
}
private void initUI() {
RelativeLayout rlayout = new RelativeLayout( this);
int id = 100;
/** 添加一个TextView */
TextView textView1 = new TextView( this);
/** android:id="" */
textView1.setId(id);
/** android:text="用户名:" */
textView1.setText("用户名:");
/** android:layout_width="wrap_content"
android:layout_height="wrap_content" */
RelativeLayout.LayoutParams textParams1 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
/** android:layout_alignParentLeft="true" */
textParams1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rlayout.addView(textView1, textParams1);
//
int id1 = 200;
EditText userEdit = new EditText( this);
userEdit.setId(id1);
RelativeLayout.LayoutParams EditParams1 = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
/** android:layout_toRightOf="id的值" */
EditParams1.addRule(RelativeLayout.RIGHT_OF, id);
rlayout.addView(userEdit, EditParams1);
//
int Id = 300;
TextView textView2 = new TextView( this);
textView2.setId(Id);
textView2.setText("密码 :");
RelativeLayout.LayoutParams TextParams2 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextParams2.addRule(RelativeLayout.BELOW, id1);
rlayout.addView(textView2, TextParams2);
//
int Id1 = 400;
EditText passEdit = new EditText( this);
passEdit.setId(Id1);
RelativeLayout.LayoutParams EditParams2 = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
EditParams2.addRule(RelativeLayout.BELOW, id1);
EditParams2.addRule(RelativeLayout.RIGHT_OF, Id);
rlayout.addView(passEdit, EditParams2);
//
int Id2 = 500;
Button login = new Button( this);
login.setId(Id2);
login.setText("登陆");
RelativeLayout.LayoutParams loginParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
loginParams.addRule(RelativeLayout.BELOW, Id1);
loginParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlayout.addView(login, loginParams);
//
Button insert = new Button( this);
insert.setText("注册");
RelativeLayout.LayoutParams insertParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
insertParams.addRule(RelativeLayout.BELOW, Id1);
insertParams.addRule(RelativeLayout.LEFT_OF, Id2);
rlayout.addView(insert, insertParams);
setContentView(rlayout);
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class LoginRelativeActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initUI();
}
private void initUI() {
RelativeLayout rlayout = new RelativeLayout( this);
int id = 100;
/** 添加一个TextView */
TextView textView1 = new TextView( this);
/** android:id="" */
textView1.setId(id);
/** android:text="用户名:" */
textView1.setText("用户名:");
/** android:layout_width="wrap_content"
android:layout_height="wrap_content" */
RelativeLayout.LayoutParams textParams1 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
/** android:layout_alignParentLeft="true" */
textParams1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rlayout.addView(textView1, textParams1);
//
int id1 = 200;
EditText userEdit = new EditText( this);
userEdit.setId(id1);
RelativeLayout.LayoutParams EditParams1 = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
/** android:layout_toRightOf="id的值" */
EditParams1.addRule(RelativeLayout.RIGHT_OF, id);
rlayout.addView(userEdit, EditParams1);
//
int Id = 300;
TextView textView2 = new TextView( this);
textView2.setId(Id);
textView2.setText("密码 :");
RelativeLayout.LayoutParams TextParams2 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextParams2.addRule(RelativeLayout.BELOW, id1);
rlayout.addView(textView2, TextParams2);
//
int Id1 = 400;
EditText passEdit = new EditText( this);
passEdit.setId(Id1);
RelativeLayout.LayoutParams EditParams2 = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
EditParams2.addRule(RelativeLayout.BELOW, id1);
EditParams2.addRule(RelativeLayout.RIGHT_OF, Id);
rlayout.addView(passEdit, EditParams2);
//
int Id2 = 500;
Button login = new Button( this);
login.setId(Id2);
login.setText("登陆");
RelativeLayout.LayoutParams loginParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
loginParams.addRule(RelativeLayout.BELOW, Id1);
loginParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlayout.addView(login, loginParams);
//
Button insert = new Button( this);
insert.setText("注册");
RelativeLayout.LayoutParams insertParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
insertParams.addRule(RelativeLayout.BELOW, Id1);
insertParams.addRule(RelativeLayout.LEFT_OF, Id2);
rlayout.addView(insert, insertParams);
setContentView(rlayout);
}
}
效果图:
![](https://i-blog.csdnimg.cn/blog_migrate/1946e809bfedab0efe2f0d8d742ad45d.gif)
表格布局:
效果图:
线性布局:
package cn.csdn.codeui;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class LoginTableActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initUI();
}
private void initUI() {
// /表格布局
TableLayout tlayout = new TableLayout( this);
tlayout.setColumnStretchable(1, true);
// /行
TableRow tableRow1 = new TableRow( this);
TextView textView1 = new TextView( this);
textView1.setText("用户名:");
tableRow1.addView(textView1);
EditText userEdit = new EditText( this);
tableRow1.addView(userEdit);
tlayout.addView(tableRow1);
TableRow tableRow2 = new TableRow( this);
TextView textView2 = new TextView( this);
textView2.setText("密码:");
tableRow2.addView(textView2);
EditText passEdit = new EditText( this);
tableRow2.addView(passEdit);
tlayout.addView(tableRow2);
TableRow tableRow3 = new TableRow( this);
Button btn0 = new Button( this);
btn0.setText("登录");
tableRow3.addView(btn0);
Button btn1 = new Button( this);
btn1.setText("注册");
tableRow3.addView(btn1);
tlayout.addView(tableRow3);
setContentView(tlayout);
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class LoginTableActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initUI();
}
private void initUI() {
// /表格布局
TableLayout tlayout = new TableLayout( this);
tlayout.setColumnStretchable(1, true);
// /行
TableRow tableRow1 = new TableRow( this);
TextView textView1 = new TextView( this);
textView1.setText("用户名:");
tableRow1.addView(textView1);
EditText userEdit = new EditText( this);
tableRow1.addView(userEdit);
tlayout.addView(tableRow1);
TableRow tableRow2 = new TableRow( this);
TextView textView2 = new TextView( this);
textView2.setText("密码:");
tableRow2.addView(textView2);
EditText passEdit = new EditText( this);
tableRow2.addView(passEdit);
tlayout.addView(tableRow2);
TableRow tableRow3 = new TableRow( this);
Button btn0 = new Button( this);
btn0.setText("登录");
tableRow3.addView(btn0);
Button btn1 = new Button( this);
btn1.setText("注册");
tableRow3.addView(btn1);
tlayout.addView(tableRow3);
setContentView(tlayout);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/c19b73f020decfb4460330f135304a20.gif)
线性布局:
package cn.csdn.codeui;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class LoginLinearActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
init();
}
private void init() {
// 线性布局
LinearLayout linearLayout = new LinearLayout( this);
/** android:orientation="vertical" */
linearLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
//
TextView userText = new TextView( this);
userText.setText("用户名:");
LayoutParams userTextParams = new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
linearLayout.addView(userText, userTextParams);
//
EditText userEdit = new EditText( this);
LayoutParams userEditParams = new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
linearLayout.addView(userEdit, userEditParams);
//
TextView passText = new TextView( this);
passText.setText("密码:");
LayoutParams passTextParams = new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
linearLayout.addView(passText, passTextParams);
//
EditText passEdit = new EditText( this);
LayoutParams passEditParams = new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
linearLayout.addView(passEdit, passEditParams);
//
Button login = new Button( this);
login.setText("登陆");
LayoutParams loginParams = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
linearLayout.addView(login, loginParams);
//
Button insert = new Button( this);
insert.setText("注册");
LayoutParams insertParams = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
linearLayout.addView(insert, insertParams);
setContentView(linearLayout, layoutParams);
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class LoginLinearActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
init();
}
private void init() {
// 线性布局
LinearLayout linearLayout = new LinearLayout( this);
/** android:orientation="vertical" */
linearLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
//
TextView userText = new TextView( this);
userText.setText("用户名:");
LayoutParams userTextParams = new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
linearLayout.addView(userText, userTextParams);
//
EditText userEdit = new EditText( this);
LayoutParams userEditParams = new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
linearLayout.addView(userEdit, userEditParams);
//
TextView passText = new TextView( this);
passText.setText("密码:");
LayoutParams passTextParams = new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
linearLayout.addView(passText, passTextParams);
//
EditText passEdit = new EditText( this);
LayoutParams passEditParams = new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
linearLayout.addView(passEdit, passEditParams);
//
Button login = new Button( this);
login.setText("登陆");
LayoutParams loginParams = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
linearLayout.addView(login, loginParams);
//
Button insert = new Button( this);
insert.setText("注册");
LayoutParams insertParams = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
linearLayout.addView(insert, insertParams);
setContentView(linearLayout, layoutParams);
}
}
效果图:
url:
http://greatverve.cnblogs.com/archive/2012/01/06/android-code-ui.html
![](https://i-blog.csdnimg.cn/blog_migrate/9bb5099435f1fb6775a7e821f6e2dafb.gif)