实现一个登陆界面:
相对布局:
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:>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:>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);
}
}
效果图:

表格布局:
效果图:

线性布局:
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);
}
}
线性布局:
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