在Android中,可以自定义类,继承ViewGroup等容器类,以实现自己需要的布局显示。如果你在ViewGroup中增加了控件,却无法显示出 来,那么下面这个例子,就可以用来参考了。(主要是要实现onLayout()方法,在这个方法中,对每个子控件进行measure(),然后再布局。)

java代码:

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. *
* @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: //
}
}
}
}

Android ViewGroup等容器控件的使用的更多相关文章

  1. Android -- ViewGroup源码分析+自定义

    1,我们前三篇博客了解了一下自定义View的基本方法和流程 从源码的角度一步步打造自己的TextView 深入了解自定义属性 onMeasure()源码分析 之前,我们只是学习过自定义View,其实自 ...

  2. Android ViewGroup onInterceptTouchEvent

    public boolean onInterceptTouchEvent (MotionEvent ev) Implement this method to intercept all touch s ...

  3. Android ViewGroup拦截触摸事件具体解释

    前言 在自己定义ViewGroup中.有时候须要实现触摸事件拦截.比方ListView下拉刷新就是典型的触摸事件拦截的样例. 触摸事件拦截就是在触摸事件被parent view拦截,而不会分发给其ch ...

  4. Android ViewGroup使用小结

    ViewGroup定义 在api中是这么描写叙述ViewGroup的:A ViewGroup is a special view that can contain other views. 依据意思我 ...

  5. Android ViewGroup点击效果(背景色)

    在开发Android应用的界面时,我们必然会用到本文ViewGroup,尤其是FrameLayout,LinearLayout,RelativeLayout等ViewGroup的子类: 在一些情况下, ...

  6. android ViewGroup事件分发机制

    1:事件分销过程 自定义一个LinearLayout,重写dispatchTouchEvent onInterceptTouchEvent onTouchEvent,定义一个按键重写dispathcT ...

  7. Android——ViewGroup的一个用法实例(转载)

    找了很久,终于找到了. <?xml version="1.0" encoding="UTF-8" ?> <merge xmlns:androi ...

  8. android ViewGroup getChildDrawingOrder与 isChildrenDrawingOrderEnabled()

    getChildDrawingOrder与 isChildrenDrawingOrderEnabled()是属于ViewGroup的方法.   getChildDrawingOrder 用于 返回当前 ...

  9. 认识一下Android 事件分发机制

    1.引子 由于android是采用分层布局(可以想象成PS时的图层概念一样),这样才可以在有限大小的手机屏幕上完成一些复杂的操作.当手指点击屏幕开始,这些动作在各层之间如何传递?就引出了Android ...

随机推荐

  1. 传智播客京东商城移动web开发

    1.源码笔记 我的源码+笔记(很重要):链接: https://pan.baidu.com/s/1eScieps 密码: 3vyr 感谢传智播客项目相关视频:1.6天链接: https://pan.b ...

  2. poj2176 Folding【区间DP】

    Folding Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1841   Accepted: 642   Special ...

  3. HotSpot VM

    1.4.2 Sun HotSpot VM_深入理解Java虚拟机:JVM高级特性与最佳实践(第2版)_红黑联盟读书频道 http://book.2cto.com/201306/25434.html 提 ...

  4. avaScript 的基础学习(一)

    JavaScript概述 JavaScript由三部分组成: JavaScript的基础 JS的引入方式: JS的数据类型: 运算符: 流程控制: JavaScript的对象 BOM对象 DOM Ev ...

  5. Python开发【前端】:Ajax(二)

    原生Ajax.JQuery.伪Ajax三种方式使用优先级 如果发送的是[普通数据] jQuery XMLHttpRequest iframe 如果发送的是[文件] iframe jQuery(Form ...

  6. 双态运维分享之二: 服务型CMDB的消费场景

    近年来,CMDB在IT运维管理中的价值逐步得到认可,使用CMDB的期望值也日益增长.然而,CMDB实施和维护的高成本却一直是建设者们的痛点.那么今天,我们来探讨一下如何通过消费来持续驱动CMDB的逐步 ...

  7. 转载:Why using Single Root I/O Virtualization (SR-IOV) can help improve I/O performance and Reduce Costs

    Introduction While server virtualization is being widely deployed in an effort to reduce costs and o ...

  8. requests获取所有状态码

    requests获取所有状态码 requests默认是不会获取301/302的状态码的.可以设置allow_redirects=False,这样就可以获取所有的状态码了 import requests ...

  9. (2.13)Mysql之SQL基础——触发器

    (2.13)Mysql之SQL基础——触发器 关键词:Mysql触发器 1.一般形式 -- 0.查看触发器[1]SHOW TRIGGERS;[2]SELECT * FROM `information_ ...

  10. shell export 命令

    export 命令作用是 把变量导出 也可以用export来定义环境变量 导入 定义的变量 这样的话类似于python面向对象的self.变量 一样 在脚本到处调用这个变量