实现一个登陆界面:

相对布局:

  1. package cn.csdn.codeui;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.ViewGroup.LayoutParams;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. import android.widget.RelativeLayout;
  8. import android.widget.TextView;
  9. public class LoginRelativeActivity extends Activity {
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. initUI();
  13. }
  14. private void initUI() {
  15. RelativeLayout rlayout = new RelativeLayout(this);
  16. int id = 100;
  17. /**添加一个TextView*/
  18. TextView textView1 = new TextView(this);
  19. /**android:id=""*/
  20. textView1.setId(id);
  21. /**android:text="用户名:"*/
  22. textView1.setText("用户名:");
  23. /**android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"*/
  25. RelativeLayout.LayoutParams textParams1 = new RelativeLayout.LayoutParams(
  26. LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  27. /**android:layout_alignParentLeft="true"*/
  28. textParams1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
  29. rlayout.addView(textView1, textParams1);
  30. //////////
  31. int id1 = 200;
  32. EditText userEdit = new EditText(this);
  33. userEdit.setId(id1);
  34. RelativeLayout.LayoutParams EditParams1 = new RelativeLayout.LayoutParams(
  35. LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
  36. /**android:layout_toRightOf="id的值"*/
  37. EditParams1.addRule(RelativeLayout.RIGHT_OF, id);
  38. rlayout.addView(userEdit, EditParams1);
  39. //////////
  40. int Id = 300;
  41. TextView textView2 = new TextView(this);
  42. textView2.setId(Id);
  43. textView2.setText("密码 :");
  44. RelativeLayout.LayoutParams TextParams2 = new RelativeLayout.LayoutParams(
  45. LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  46. TextParams2.addRule(RelativeLayout.BELOW, id1);
  47. rlayout.addView(textView2, TextParams2);
  48. //////////
  49. int Id1 = 400;
  50. EditText passEdit = new EditText(this);
  51. passEdit.setId(Id1);
  52. RelativeLayout.LayoutParams EditParams2 = new RelativeLayout.LayoutParams(
  53. LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
  54. EditParams2.addRule(RelativeLayout.BELOW, id1);
  55. EditParams2.addRule(RelativeLayout.RIGHT_OF, Id);
  56. rlayout.addView(passEdit, EditParams2);
  57. //////////
  58. int Id2 = 500;
  59. Button login = new Button(this);
  60. login.setId(Id2);
  61. login.setText("登陆");
  62. RelativeLayout.LayoutParams loginParams = new RelativeLayout.LayoutParams(
  63. LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  64. loginParams.addRule(RelativeLayout.BELOW, Id1);
  65. loginParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
  66. rlayout.addView(login, loginParams);
  67. //////////
  68. Button insert = new Button(this);
  69. insert.setText("注册");
  70. RelativeLayout.LayoutParams insertParams = new RelativeLayout.LayoutParams(
  71. LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  72. insertParams.addRule(RelativeLayout.BELOW, Id1);
  73. insertParams.addRule(RelativeLayout.LEFT_OF, Id2);
  74. rlayout.addView(insert, insertParams);
  75. setContentView(rlayout);
  76. }
  77. }

效果图:

表格布局:

  1. package cn.csdn.codeui;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.ViewGroup.LayoutParams;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. import android.widget.TableLayout;
  8. import android.widget.TableRow;
  9. import android.widget.TextView;
  10. public class LoginTableActivity extends Activity {
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. initUI();
  14. }
  15. private void initUI() {
  16. ///表格布局
  17. TableLayout tlayout = new TableLayout(this);
  18. tlayout.setColumnStretchable(1, true);
  19. ///行
  20. TableRow tableRow1 = new TableRow(this);
  21. TextView textView1 = new TextView(this);
  22. textView1.setText("用户名:");
  23. tableRow1.addView(textView1);
  24. EditText userEdit = new EditText(this);
  25. tableRow1.addView(userEdit);
  26. tlayout.addView(tableRow1);
  27. TableRow tableRow2 = new TableRow(this);
  28. TextView textView2 = new TextView(this);
  29. textView2.setText("密码:");
  30. tableRow2.addView(textView2);
  31. EditText passEdit = new EditText(this);
  32. tableRow2.addView(passEdit);
  33. tlayout.addView(tableRow2);
  34. TableRow tableRow3 = new TableRow(this);
  35. Button btn0 = new Button(this);
  36. btn0.setText("登录");
  37. tableRow3.addView(btn0);
  38. Button btn1 = new Button(this);
  39. btn1.setText("注册");
  40. tableRow3.addView(btn1);
  41. tlayout.addView(tableRow3);
  42. setContentView(tlayout);
  43. }
  44. }

效果图:

线性布局:

  1. package cn.csdn.codeui;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.ViewGroup.LayoutParams;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. import android.widget.LinearLayout;
  8. import android.widget.TextView;
  9. public class LoginLinearActivity extends Activity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. // TODO Auto-generated method stub
  13. super.onCreate(savedInstanceState);
  14. init();
  15. }
  16. private void init() {
  17. //线性布局
  18. LinearLayout linearLayout = new LinearLayout(this);
  19. /**android:orientation="vertical"*/
  20. linearLayout.setOrientation(LinearLayout.VERTICAL);
  21. LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,
  22. LayoutParams.FILL_PARENT);
  23. //////////
  24. TextView userText = new TextView(this);
  25. userText.setText("用户名:");
  26. LayoutParams userTextParams = new LayoutParams(
  27. LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
  28. linearLayout.addView(userText, userTextParams);
  29. //////////
  30. EditText userEdit = new EditText(this);
  31. LayoutParams userEditParams = new LayoutParams(
  32. LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
  33. linearLayout.addView(userEdit, userEditParams);
  34. //////////
  35. TextView passText = new TextView(this);
  36. passText.setText("密码:");
  37. LayoutParams passTextParams = new LayoutParams(
  38. LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
  39. linearLayout.addView(passText, passTextParams);
  40. //////////
  41. EditText passEdit = new EditText(this);
  42. LayoutParams passEditParams = new LayoutParams(
  43. LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
  44. linearLayout.addView(passEdit, passEditParams);
  45. //////////
  46. Button login = new Button(this);
  47. login.setText("登陆");
  48. LayoutParams loginParams = new LayoutParams(LayoutParams.FILL_PARENT,
  49. LayoutParams.WRAP_CONTENT);
  50. linearLayout.addView(login, loginParams);
  51. //////////
  52. Button insert = new Button(this);
  53. insert.setText("注册");
  54. LayoutParams insertParams = new LayoutParams(LayoutParams.FILL_PARENT,
  55. LayoutParams.WRAP_CONTENT);
  56. linearLayout.addView(insert, insertParams);
  57. setContentView(linearLayout, layoutParams);
  58. }
  59. }

效果图:

最新文章

  1. codevs 2287 火车站
  2. WinCE项目应用之RM905a+医用放射性核素活度计
  3. eclipse使用tips-Toggle Mark Occurrences 颜色更改
  4. 【转载】 Pyqt 利用QDataStream对文件进行存取
  5. (笔记)angular 包含关系的controller参数传递
  6. 相对布局RelativeLayout
  7. Asp.net Json 解析 与 直接用ip访问返回josn
  8. bat 批处理脚本
  9. MVC-@html.ActionLink的几种参数格式
  10. 地址重写--Java中urlrewriter的使用
  11. java基础系列--Date类
  12. MySQL grant命令使用
  13. 前端自动化测试神器-Katalon进阶用法
  14. nginx,maven
  15. 关于ico图标
  16. Hibernate中的实体规则、对象状态和进阶-一级缓存
  17. 静态和实例方法区别-java
  18. Concurrency in C# Cookbook 笔记
  19. php7 安装mssql 扩展
  20. Spring Cloud 各组件调优参数

热门文章

  1. apt-get和apt-cache命令实例展示
  2. Java并发教程(Oracle官方资料)
  3. 仿LOL项目开发第二天
  4. mvn sonar:sonar在jenkins步骤的执行位置影响执行结果
  5. WEB渗透之对于开了3389远程连接不上的总结
  6. iFrame的妙用
  7. Microsoft Visual C++ 14.0 is required问题的解决或warning: Debugger speedups using cython not found
  8. RHEL7.0 配置网络IP的三种方法
  9. 将项目的版本控制从Vss迁移到Git
  10. 自定义错误信息并写入到Elmah