Android中,可以自定义类,继承ViewGroup等容器类,以实现自己需要的布局显示。

 

如果你在ViewGroup中增加了控件,却无法显示出来,那么下面这个例子,就可以用来参考了。

 

(主要是要实现onLayout()方法,在这个方法中,对每个子控件进行measure(),然后再布局。

 

package com.arui; import android.content.Context; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.Button; /** * Example for using ViewGroup. * * @author http://blog.csdn.net/arui319 * @version 2010/09/07 * */ public class MyViewGroup extends ViewGroup { public MyViewGroup(Context context) { super(context); this.initOtherComponent(context); } private void initOtherComponent(Context context) { Button aBtn = new Button(context); // set id 1 aBtn.setId(1); aBtn.setText("a btn"); this.addView(aBtn); Button bBtn = new Button(context); // set id 2 bBtn.setId(2); bBtn.setText("b btn"); this.addView(bBtn); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View child = getChildAt(i); switch (child.getId()) { case 1: // 1 is aBtn Log.d("MyViewGroup", "btn1 setting"); child.setVisibility(View.VISIBLE); child.measure(r - l, b - t); child.layout(0, 0, child.getMeasuredWidth(), child .getMeasuredHeight()); break; case 2: // 2 is bBtn Log.d("MyViewGroup", "btn2 setting"); child.setVisibility(View.VISIBLE); child.measure(r - l, b - t); child.layout(0, 50, child.getMeasuredWidth(), child .getMeasuredHeight() + 50); break; default: // } } } }  

 

更多相关文章

  1. Instrumentation 框架简介
  2. Android(安卓)系统启动过程详解
  3. 如何实现Android(安卓)APP静默安装
  4. Android之UI学习篇八:使用GridView实现九宫格的菜单
  5. Android(安卓)实用工具Hierarchy Viewer实战
  6. Android(安卓)的invalidate 与postInvalidate的用法
  7. Flutter 混合开发(Android)Flutter跟Native相互通信
  8. 浅谈Java中Collections.sort对List排序的两种方法
  9. Python list sort方法的具体使用

随机推荐

  1. Android中遍历文件夹、比较文件类型测试
  2. android 触摸手指动作放大和缩小图片
  3. android全屏显示
  4. Android实现文件上传功能
  5. Android(安卓)全屏
  6. android开机动画bootanimation
  7. 提示Android(安卓)dependency 'com.andro
  8. 选择框在右边的单选按钮。。
  9. android 通知Notification的使用小实例(振
  10. Android(安卓)简单视频播放器(破烂版,后续