首页 > 系统 > Android > 正文

基于Android代码实现常用布局

2019-10-24 20:34:19
字体:
来源:转载
供稿:网友

大家在日常中经常见到用xml文件实现android常用布局,但是大家知道如何用代码实现呢?使用代码实现可以帮助我们学习sdk api,所以小编把我日常整理些关于android常用布局代码实现分享给大家

关于 android 常用布局,利用 XML 文件实现已经有很多的实例了。但如何利用代码实现呢?当然利用代码实现没有太大的必要,也是不提倡的,但我觉得利用代码实现这些布局,可以更好的了解 SDK API ,所以在此也整理一些,和大家分享一下。

首先看一下,布局所对应的类的 API 继承图:

基于Android代码实现常用布局

android常用布局的代码实现所有的布局都会对应相关的类,这些类都是继承自 android.view.ViewGroup 类的。而 LinearLayout,RelativeLayout 都是在 android.widget 包里的。另外,TableLayout 是继承自 LinearLayout.

下面直接贴代码了。

 

 
  1. // 利用代码设置 线性布局 
  2. private void setLinearLayout(){ 
  3. LinearLayout llayout = new LinearLayout(this); 
  4. llayout.setOrientation(LinearLayout.VERTICAL); // 设置线性布局的排列方式 
  5. TextView textView = new TextView(this); 
  6. textView.setText("代码实现的线性布局"); 
  7. textView.setTextColor(Color.RED); 
  8. textView.setGravity(Gravity.CENTER); // 设置文本内容的对齐方式 
  9. LinearLayout.LayoutParams ll_lpara = new LinearLayout.LayoutParams(MP,WC); 
  10. // ll_lpara.gravity = Gravity.CENTER_HORIZONTAL; // 设置控件在布局中的对齐方式 
  11. llayout.addView(textView,ll_lpara); 
  12. Button btn = new Button(this); 
  13. btn.setText("按钮"); 
  14. llayout.addView(btn,ll_lpara); // 按指定属性添加控件 
  15. setContentView(llayout);  

实现效果图:

基于Android代码实现常用布局

=========================================================================

 

 
  1. // 利用代码设置 相对布局 
  2. private void setRelativeLayout(){ 
  3. RelativeLayout rlayout = new RelativeLayout(this); 
  4. rlayout.setPadding(10, 10, 10, 10); // 单位: pixels 
  5. int textViewID = 100; 
  6. TextView textView = new TextView(this); 
  7. textView.setId(textViewID); 
  8. textView.setText("请输入:"); 
  9. RelativeLayout.LayoutParams rl_lpara1 = new RelativeLayout.LayoutParams(MP, WC); 
  10. rlayout.addView(textView, rl_lpara1); 
  11. int editTextID = 200; 
  12. EditText editText = new EditText(this); 
  13. editText.setId(editTextID); 
  14. editText.setBackgroundResource(android.R.drawable.editbox_background); // 设置背景 , 同android:backgroumd 
  15. RelativeLayout.LayoutParams rl_lpara2 = new RelativeLayout.LayoutParams(MP, WC); 
  16. rl_lpara2.addRule(RelativeLayout.BELOW,textViewID); // 设置相对属性,需先指定相对控件的ID 
  17. rlayout.addView(editText, rl_lpara2);  
  18. int backBtnID = 300; 
  19. Button backBtn = new Button(this); 
  20. backBtn.setId(backBtnID); 
  21. backBtn.setText("返回"); 
  22. RelativeLayout.LayoutParams rl_lpara3 = new RelativeLayout.LayoutParams(WC, WC); 
  23. rl_lpara3.addRule(RelativeLayout.BELOW, editTextID); 
  24. rl_lpara3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); // 设置与父控件的相对属性 
  25. rlayout.addView(backBtn, rl_lpara3); 
  26. Button okBtn = new Button(this); 
  27. okBtn.setText("确定"); 
  28. RelativeLayout.LayoutParams rl_lpara4 = new RelativeLayout.LayoutParams(WC, WC); 
  29. rl_lpara4.addRule(RelativeLayout.LEFT_OF, backBtnID); 
  30. rl_lpara4.addRule(RelativeLayout.ALIGN_TOP,backBtnID); 
  31. rlayout.addView(okBtn, rl_lpara4); 
  32. setContentView(rlayout); 

实现效果图:

基于Android代码实现常用布局

=========================================================================

 

 
  1. // 利用代码设置 表格布局 
  2. private void setTableLayout(){ 
  3. TableLayout tlayout = new TableLayout(this); 
  4. tlayout.setColumnStretchable(2, true); // 拉长索引从0开始的第2列 
  5. TableLayout.LayoutParams tl_lpara = new TableLayout.LayoutParams(MP,WC); 
  6. // 1. TableRow 不需要设置 layout_width, layout_height 
  7. // 2. TableRow 中的控件不能设置 layout_span 属性 
  8. TableRow tr1 = new TableRow(this);  
  9. TextView textView0 = new TextView(this); 
  10. textView0.setText("第0列"); 
  11. tr1.addView(textView0);  
  12. TextView textView1 = new TextView(this); 
  13. textView1.setText("第1列"); 
  14. tr1.addView(textView1);  
  15. TextView textView2 = new TextView(this); 
  16. textView2.setText("第2列"); 
  17. textView2.setBackgroundColor(Color.CYAN); 
  18. tr1.addView(textView2);  
  19. tlayout.addView(tr1, tl_lpara);  
  20. TableRow tr2 = new TableRow(this); 
  21. Button btn0 = new Button(this); 
  22. btn0.setText("按钮0"); 
  23. tr2.addView(btn0);  
  24. Button btn1 = new Button(this); 
  25. btn1.setText("按钮1"); 
  26. tr2.addView(btn1);  
  27. Button btn2 = new Button(this); 
  28. btn2.setText("按钮2"); 
  29. tr2.addView(btn2);  
  30. Button btn3 = new Button(this); 
  31. btn3.setText("按钮3"); 
  32. tr2.addView(btn3); 
  33. tlayout.addView(tr2, tl_lpara); 
  34. setContentView(tlayout);  

实现效果图:

基于Android代码实现常用布局


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表