基于Android代码实现常用布局

所属分类: 软件编程 / Android 阅读数: 1557
收藏 0 赞 0 分享

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

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

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

下面直接贴代码了。

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

实现效果图:

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

// 利用代码设置 相对布局
 private void setRelativeLayout(){
  RelativeLayout rlayout = new RelativeLayout(this);
  rlayout.setPadding(10, 10, 10, 10); // 单位: pixels
  int textViewID = 100;
  TextView textView = new TextView(this);
  textView.setId(textViewID);
  textView.setText("请输入:");
  RelativeLayout.LayoutParams rl_lpara1 = new RelativeLayout.LayoutParams(MP, WC);
  rlayout.addView(textView, rl_lpara1);
  int editTextID = 200;
  EditText editText = new EditText(this);
  editText.setId(editTextID);
  editText.setBackgroundResource(android.R.drawable.editbox_background); // 设置背景 , 同android:backgroumd
  RelativeLayout.LayoutParams rl_lpara2 = new RelativeLayout.LayoutParams(MP, WC);
  rl_lpara2.addRule(RelativeLayout.BELOW,textViewID); // 设置相对属性,需先指定相对控件的ID
  rlayout.addView(editText, rl_lpara2);  
  int backBtnID = 300;
  Button backBtn = new Button(this);
  backBtn.setId(backBtnID);
  backBtn.setText("返回");
  RelativeLayout.LayoutParams rl_lpara3 = new RelativeLayout.LayoutParams(WC, WC);
  rl_lpara3.addRule(RelativeLayout.BELOW, editTextID);
  rl_lpara3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); // 设置与父控件的相对属性
  rlayout.addView(backBtn, rl_lpara3);
  Button okBtn = new Button(this);
  okBtn.setText("确定");
  RelativeLayout.LayoutParams rl_lpara4 = new RelativeLayout.LayoutParams(WC, WC);
  rl_lpara4.addRule(RelativeLayout.LEFT_OF, backBtnID);
  rl_lpara4.addRule(RelativeLayout.ALIGN_TOP,backBtnID);
  rlayout.addView(okBtn, rl_lpara4);
  setContentView(rlayout);
 }

实现效果图:

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

// 利用代码设置 表格布局
 private void setTableLayout(){
  TableLayout tlayout = new TableLayout(this);
  tlayout.setColumnStretchable(2, true); // 拉长索引从0开始的第2列
  TableLayout.LayoutParams tl_lpara = new TableLayout.LayoutParams(MP,WC);
  // 1. TableRow 不需要设置 layout_width, layout_height
  // 2. TableRow 中的控件不能设置 layout_span 属性
  TableRow tr1 = new TableRow(this); 
  TextView textView0 = new TextView(this);
  textView0.setText("第0列");
  tr1.addView(textView0);  
  TextView textView1 = new TextView(this);
  textView1.setText("第1列");
  tr1.addView(textView1);  
  TextView textView2 = new TextView(this);
  textView2.setText("第2列");
  textView2.setBackgroundColor(Color.CYAN);
  tr1.addView(textView2);   
  tlayout.addView(tr1, tl_lpara); 
  TableRow tr2 = new TableRow(this);
  Button btn0 = new Button(this);
  btn0.setText("按钮0");
  tr2.addView(btn0);  
  Button btn1 = new Button(this);
  btn1.setText("按钮1");
  tr2.addView(btn1);  
  Button btn2 = new Button(this);
  btn2.setText("按钮2");
  tr2.addView(btn2); 
  Button btn3 = new Button(this);
  btn3.setText("按钮3");
  tr2.addView(btn3);
  tlayout.addView(tr2, tl_lpara);
  setContentView(tlayout);  
 }

实现效果图:


更多精彩内容其他人还在看

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中的Bitmap序列化失败的解决方法

这篇文章主要介绍了Android中的Bitmap序列化失败的解决方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android自定义通用标题栏CustomTitleBar

这篇文章主要为大家详细介绍了Android自定义通用标题栏CustomTitleBar,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android组合控件自定义标题栏

这篇文章主要为大家详细介绍了Android组合控件自定义标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义复合控件实现通用标题栏

这篇文章主要为大家详细介绍了Android自定义复合控件实现通用标题栏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ExpandableListView实现简单二级列表

这篇文章主要为大家详细介绍了ExpandableListView实现简单二级列表,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多