在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. oracle数据库实例状态

    1.已启动/不装载(NOMOUNT).启动实例,但不装载数据库. 该模式用于重新创建控制文件,对控制文件进行恢复或重新创建数据库.2.已装载(MOUNT).装载数据库,但不打开数据库. 该模式用于更改 ...

  2. Linux 使用 常识记忆

    1.当系统突然死机需要重启时,打开 命令输入端口,使用快捷键 Shift +Alt +F(1或6)打开 ,然后输入 shutdown -r now 如果提示shutdown need to be ro ...

  3. nginx 重定向

    不带www跳转www 1.301: return 301 http://www.xx.com$request_uri; 2.(1)rewrite ^(.*)$ http://www.xx.com$1 ...

  4. angular自定义指令命名的那个坑

    Directive 先从定义一个简单的指令开始. 定义一个指令本质上是在HTML中通过元素.属性.类或注释来添加功能.AngularJS的内置指令都是以ng开头,如果想自定义指令,建议自定义一个前缀代 ...

  5. Navicat连接服务器上的Mysql数据库

  6. 如何在python3.5环境下安装BeautifulSoup?

    首先是安装: 1.到http://www.crummy.com/software/BeautifulSoup/网站上上下载 2.下载完成之后需要解压缩,假设放到D:/python下. 3.运行cmd, ...

  7. JS事件常用事件

    oncontextmenu对象右击 举例1: <form id="form1" name="form1" method="post" ...

  8. MySQL的redo log结构和SQL Server的log结构对比

    MySQL的redo log结构和SQL Server的log结构对比 innodb 存储引擎 mysql技术内幕 log buffer根据一定规则将内存中的log block刷写到磁盘,这个规则是 ...

  9. java-mybaits-00801-逆向工程

    1.1     什么是逆向工程 参看地址:http://www.mybatis.org/generator/index.html 使用官方网站的mapper自动生成工具mybatis-generato ...

  10. Openstack(十六)实现内外网结构

    类似于阿里云ECS主机的内外网(双网卡不通网段)的结构,最终实现内外网区分隔离. https://www.aliyun.com/product/ecs/?utm_medium=text&utm ...