Android成长之路-编码实现软件界面

 

实现一个登陆界面:

相对布局:

  1. package cn.csdn.codeui;  
  2.   
  3. import android.app.Activity;  
  4.   
  5. import android.os.Bundle;  
  6.   
  7. import android.view.ViewGroup.LayoutParams;  
  8.   
  9. import android.widget.Button;  
  10.   
  11. import android.widget.EditText;  
  12.   
  13. import android.widget.RelativeLayout;  
  14.   
  15. import android.widget.TextView;  
  16.   
  17. public class LoginRelativeActivity extends Activity {  
  18.   
  19. protected void onCreate(Bundle savedInstanceState) {  
  20.   
  21. super.onCreate(savedInstanceState);  
  22.   
  23. initUI();  
  24.   
  25. }  
  26.   
  27. private void initUI() {  
  28.   
  29. RelativeLayout rlayout = new RelativeLayout(this);  
  30.   
  31. int id = 100;  
  32.   
  33. /**添加一个TextView*/  
  34.   
  35. TextView textView1 = new TextView(this);  
  36.   
  37. /**android:id=""*/  
  38.   
  39. textView1.setId(id);  
  40.   
  41. /**android:text="用户名:"*/  
  42.   
  43. textView1.setText("用户名:");  
  44.   
  45. /**android:layout_width="wrap_content" 
  46.  
  47.            android:layout_height="wrap_content"*/  
  48.   
  49. RelativeLayout.LayoutParams textParams1 = new RelativeLayout.LayoutParams(  
  50.   
  51. LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  52.   
  53. /**android:layout_alignParentLeft="true"*/  
  54.   
  55. textParams1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);  
  56.   
  57. rlayout.addView(textView1, textParams1);  
  58.   
  59. //////////  
  60.   
  61. int id1 = 200;  
  62.   
  63. EditText userEdit = new EditText(this);  
  64.   
  65. userEdit.setId(id1);  
  66.   
  67. RelativeLayout.LayoutParams EditParams1 = new RelativeLayout.LayoutParams(  
  68.   
  69. LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);  
  70.   
  71. /**android:layout_toRightOf="id的值"*/  
  72.   
  73. EditParams1.addRule(RelativeLayout.RIGHT_OF, id);  
  74.   
  75. rlayout.addView(userEdit, EditParams1);  
  76.   
  77. //////////  
  78.   
  79. int Id = 300;  
  80.   
  81. TextView textView2 = new TextView(this);  
  82.   
  83. textView2.setId(Id);  
  84.   
  85. textView2.setText("密码 :");  
  86.   
  87. RelativeLayout.LayoutParams TextParams2 = new RelativeLayout.LayoutParams(  
  88.   
  89. LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  90.   
  91. TextParams2.addRule(RelativeLayout.BELOW, id1);  
  92.   
  93. rlayout.addView(textView2, TextParams2);  
  94.   
  95. //////////  
  96.   
  97. int Id1 = 400;  
  98.   
  99. EditText passEdit = new EditText(this);  
  100.   
  101. passEdit.setId(Id1);  
  102.   
  103. RelativeLayout.LayoutParams EditParams2 = new RelativeLayout.LayoutParams(  
  104.   
  105. LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);  
  106.   
  107. EditParams2.addRule(RelativeLayout.BELOW, id1);  
  108.   
  109. EditParams2.addRule(RelativeLayout.RIGHT_OF, Id);  
  110.   
  111. rlayout.addView(passEdit, EditParams2);  
  112.   
  113. //////////  
  114.   
  115. int Id2 = 500;  
  116.   
  117. Button login = new Button(this);  
  118.   
  119. login.setId(Id2);  
  120.   
  121. login.setText("登陆");  
  122.   
  123. RelativeLayout.LayoutParams loginParams = new RelativeLayout.LayoutParams(  
  124.   
  125. LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  126.   
  127. loginParams.addRule(RelativeLayout.BELOW, Id1);  
  128.   
  129. loginParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);  
  130.   
  131. rlayout.addView(login, loginParams);  
  132.   
  133. //////////  
  134.   
  135. Button insert = new Button(this);  
  136.   
  137. insert.setText("注册");  
  138.   
  139. RelativeLayout.LayoutParams insertParams = new RelativeLayout.LayoutParams(  
  140.   
  141. LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  142.   
  143. insertParams.addRule(RelativeLayout.BELOW, Id1);  
  144.   
  145. insertParams.addRule(RelativeLayout.LEFT_OF, Id2);  
  146.   
  147. rlayout.addView(insert, insertParams);  
  148.   
  149. setContentView(rlayout);  
  150.   
  151. }  
  152.   
  153. }  


 

效果图:

 

表格布局:

  1. package cn.csdn.codeui;  
  2.   
  3. import android.app.Activity;  
  4.   
  5. import android.os.Bundle;  
  6.   
  7. import android.view.ViewGroup.LayoutParams;  
  8.   
  9. import android.widget.Button;  
  10.   
  11. import android.widget.EditText;  
  12.   
  13. import android.widget.TableLayout;  
  14.   
  15. import android.widget.TableRow;  
  16.   
  17. import android.widget.TextView;  
  18.   
  19. public class LoginTableActivity extends Activity {  
  20.   
  21. protected void onCreate(Bundle savedInstanceState) {  
  22.   
  23. super.onCreate(savedInstanceState);  
  24.   
  25. initUI();  
  26.   
  27. }  
  28.   
  29. private void initUI() {  
  30.   
  31. ///表格布局  
  32.   
  33. TableLayout tlayout = new TableLayout(this);  
  34.   
  35. tlayout.setColumnStretchable(1, true);  
  36.   
  37. ///行  
  38.   
  39. TableRow tableRow1 = new TableRow(this);  
  40.   
  41. TextView textView1 = new TextView(this);  
  42.   
  43. textView1.setText("用户名:");  
  44.   
  45. tableRow1.addView(textView1);  
  46.   
  47. EditText userEdit = new EditText(this);  
  48.   
  49. tableRow1.addView(userEdit);  
  50.   
  51. tlayout.addView(tableRow1);  
  52.   
  53. TableRow tableRow2 = new TableRow(this);  
  54.   
  55. TextView textView2 = new TextView(this);  
  56.   
  57. textView2.setText("密码:");  
  58.   
  59. tableRow2.addView(textView2);  
  60.   
  61. EditText passEdit = new EditText(this);  
  62.   
  63. tableRow2.addView(passEdit);  
  64.   
  65. tlayout.addView(tableRow2);  
  66.   
  67. TableRow tableRow3 = new TableRow(this);  
  68.   
  69. Button btn0 = new Button(this);  
  70.   
  71. btn0.setText("登录");  
  72.   
  73. tableRow3.addView(btn0);  
  74.   
  75. Button btn1 = new Button(this);  
  76.   
  77. btn1.setText("注册");  
  78.   
  79. tableRow3.addView(btn1);  
  80.   
  81. tlayout.addView(tableRow3);  
  82.   
  83. setContentView(tlayout);  
  84.   
  85. }  
  86.   
  87. }  


 

效果图:

 

线性布局:

  1. package cn.csdn.codeui;  
  2.   
  3. import android.app.Activity;  
  4.   
  5. import android.os.Bundle;  
  6.   
  7. import android.view.ViewGroup.LayoutParams;  
  8.   
  9. import android.widget.Button;  
  10.   
  11. import android.widget.EditText;  
  12.   
  13. import android.widget.LinearLayout;  
  14.   
  15. import android.widget.TextView;  
  16.   
  17. public class LoginLinearActivity extends Activity {  
  18.   
  19. @Override  
  20.   
  21. protected void onCreate(Bundle savedInstanceState) {  
  22.   
  23. // TODO Auto-generated method stub  
  24.   
  25. super.onCreate(savedInstanceState);  
  26.   
  27. init();  
  28.   
  29. }  
  30.   
  31. private void init() {  
  32.   
  33. //线性布局  
  34.   
  35. LinearLayout linearLayout = new LinearLayout(this);  
  36.   
  37. /**android:orientation="vertical"*/  
  38.   
  39. linearLayout.setOrientation(LinearLayout.VERTICAL);  
  40.   
  41. LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,  
  42.   
  43. LayoutParams.FILL_PARENT);  
  44.   
  45. //////////  
  46.   
  47. TextView userText = new TextView(this);  
  48.   
  49. userText.setText("用户名:");  
  50.   
  51. LayoutParams userTextParams = new LayoutParams(  
  52.   
  53. LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);  
  54.   
  55. linearLayout.addView(userText, userTextParams);  
  56.   
  57.         //////////  
  58.   
  59. EditText userEdit = new EditText(this);  
  60.   
  61. LayoutParams userEditParams = new LayoutParams(  
  62.   
  63. LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);  
  64.   
  65. linearLayout.addView(userEdit, userEditParams);  
  66.   
  67.         //////////  
  68.   
  69. TextView passText = new TextView(this);  
  70.   
  71. passText.setText("密码:");  
  72.   
  73. LayoutParams passTextParams = new LayoutParams(  
  74.   
  75. LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);  
  76.   
  77. linearLayout.addView(passText, passTextParams);  
  78.   
  79.         //////////  
  80.   
  81. EditText passEdit = new EditText(this);  
  82.   
  83. LayoutParams passEditParams = new LayoutParams(  
  84.   
  85. LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);  
  86.   
  87. linearLayout.addView(passEdit, passEditParams);  
  88.   
  89.         //////////  
  90.   
  91. Button login = new Button(this);  
  92.   
  93. login.setText("登陆");  
  94.   
  95. LayoutParams loginParams = new LayoutParams(LayoutParams.FILL_PARENT,  
  96.   
  97. LayoutParams.WRAP_CONTENT);  
  98.   
  99. linearLayout.addView(login, loginParams);  
  100.   
  101.         //////////  
  102.   
  103. Button insert = new Button(this);  
  104.   
  105. insert.setText("注册");  
  106.   
  107. LayoutParams insertParams = new LayoutParams(LayoutParams.FILL_PARENT,  
  108.   
  109. LayoutParams.WRAP_CONTENT);  
  110.   
  111. linearLayout.addView(insert, insertParams);  
  112.   
  113. setContentView(linearLayout, layoutParams);  
  114.   
  115. }  
  116.   
  117. }  


 

效果图:

posted @ 2014-07-30 14:19  新感觉  阅读(194)  评论(0编辑  收藏  举报