在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

    Android ViewGroup class is used to create the screen views. In this tutorial, we will learn about th ...

  2. Android ViewGroup的draw和onDraw的调用时机

    Android ViewGroup的draw和onDraw的调用时机 View.draw和View.onDraw的调用关系 首先,View.draw和View.onDraw是两个不同的方法,只有Vie ...

  3. android viewgroup 事件,android中viewgroup的事件传递分析

    在上一篇中我们分析了从view的dispatchTouchEvent到onTouchListener的onTouch回调到onTouchEvent到onClickLisener的onClickandr ...

  4. Android ViewGroup事件分发机制

    理~ 1.案例 首先我们接着上一篇的代码,在代码中添加一个自定义的LinearLayout: [java] view plaincopy package com.example.zhy_event03 ...

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

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

  6. android viewgroup点击变色,Android ViewGroup事件分发

    上篇文章已经分析了Android的Touch事件分发.如果没看的建议先看一下.Android View的Touch事件分发. 接下来我们开始写几种场景,得出一个初步的执行顺序,然后我们按照这个顺序开始 ...

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

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

  8. Android ViewGroup介绍+实例,大厂架构师经验分享

    //每一行的高度,累加至height int lineHeight = 0; int count = getChildCount(); int left = getPaddingLeft(); int ...

  9. Android ViewGroup拦截触摸事件详解

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

最新文章

  1. linux svn客户端的使用
  2. tomcat安装及虚拟web主机
  3. 存在于实数域的微观粒子3-∂f(x)/ ∂x=f(x).f(-x)
  4. 又一个高等级数据中心着火了!
  5. flex如何做响应式设计_响应式设计-您做错了!
  6. 什么是量子计算机?用一个简单例子来解释
  7. mysql用in查询list_Mysql优化器对in list的处理
  8. pads导出坐标文件html,【教程】PADS如何导出SMT贴片机用的坐标文件
  9. php 5.4 mysql 5.7_致命错误:在将PHP 5.4.22和MySQL 5.5与Apache 2.4.7连接时调用未定义的函数mysqli_connect()...
  10. oracle votedisk ocr,Oracle 11gR2 RAC OCR和votingdisk故障恢复案例
  11. 2015第35周六转相见恨晚的知识列表
  12. Windows、Linux下安装Redis图文教程
  13. 整合HikariCP
  14. html看图识颜色,canvas 识别图片颜色 解析图片颜色
  15. 新计算机c盘太小,电脑C盘太小,F盘太大,怎么重新调整分区容量?
  16. 通过无线网络实现两台计算机共享打印机共享,同一WiFi环境中两台电脑共享打印机技巧方法...
  17. 汇编指令中 Rd Rm Rn Ra 的 具体含义 ?
  18. pikachu漏洞平台靶场练习 总结 wp
  19. 《markdown》语雀导出markdown的图片外链问题
  20. k宝无法连接计算机,农行K宝无法识别

热门文章

  1. python画小猪佩奇
  2. layui ajax流加载,layui中流加载layui.flow
  3. 模拟linux磁盘满,linux 磁盘满了简单处理一下
  4. pythonscrapy爬虫_Python 爬虫:Scrapy 实例(二)
  5. springBoot @Scheduled多任务同时开始执行
  6. Rust编写的新终端多路复用器
  7. 三次握手和四次挥手图解_图解 TCP 三次握手和四次挥手
  8. python怎么玩pdf_最全总结!聊聊 python 操作PDF的几种方法
  9. 【学习笔记】《概率与期望全家桶》(ACM / OI)概率与期望 / 概率论知识小结
  10. cfa英语不好的怎么学_英语和数学不好,还怎么学CFA?!