[转载] Android中自定义ViewGroup
2014-03-25 15:34
369 查看
一、ViewGroup概述
研究ViewGroup之前,我们先来看看ViewGroup的介绍:
Android关于ViewGroup的解释还是比较清楚的,通过这个我们可以看出几点:
1、ViewGroup是一个容器,而这个容器是继承与View的。
2、ViewGroup是一个基类,并且是Layout和一些View组件的基类。
等等,不一而足,眼界有多高相信看到的就有多远,呵呵。
二、ViewGroup的三个方法
在继承ViewGroup时有三个重要的方法,下面我们就来看看:
1、onLayout方法
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
}
在我们继承ViewGroup时会在除了构造函数之外提供这个方法,我们可以看到,在ViewGroup的源代码中方法是这样定义的,也就是父类没有提供方法的内容,需要我们自己实现。
当View要为所有子对象分配大小和位置时,调用此方法
2、addView方法
public void addView(View child) {
addView(child, -1);
}
这个方法是用来想View容器中添加组件用的。我们可以使用这个方法想这个ViewGroup中添加组件。
3、getChildAt方法
public View getChildAt(int index) {
try {
return mChildren[index];
} catch (IndexOutOfBoundsException ex) {
return null;
}
}
这个方法用来返回指定位置的View。
注意:ViewGroup中的View是从0开始计数的。
可以说我们自定义ViewGroup时这三个方法是至关重要的,下面我们就来看看自定义ViewGroup使用。
三、一个小Demo
我们新建一个叫AndroidViewGroup的工程,Activity起名为MainActivity。在写一个继承于ViewGroup的类,名叫HelloViewGroup。
-->HelloViewGroup类
public class HelloViewGroup extends ViewGroup {
public HelloViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public HelloViewGroup(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
}
}
-->MainActivity类
public class MainActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new HelloViewGroup(this));
}
}
这时你可以运行一下,发现屏幕除了状态栏了那个Label是一片黑,呵呵。下面我们来修改代码,让自己的ViewGroup火起来。
我们新建一个名叫myAddView的方法,这个方法用来向ViewGroup中添加组件:
/**
* 添加View的方法
* */
public void myAddView(){
ImageView mIcon = new ImageView(mContext);
mIcon.setImageResource(R.drawable.haha);
addView(mIcon);
}
然后我们修改onLayout方法:
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
View v = getChildAt(0);
v.layout(l, t, r, b);
}
然后我们看看运行效果:
研究ViewGroup之前,我们先来看看ViewGroup的介绍:
/** * A ViewGroup is a special view that can contain other views * (called children.) The view group is the base class for layouts and views * containers. This class also defines the * android.view.ViewGroup.LayoutParams class which serves as the base * class for layouts parameters. |
一个ViewGroup是一个可以包含其他view的特别的View,ViewGroup是各个Layout和View组件的基类。(翻译的不太好,能看懂就行了) |
1、ViewGroup是一个容器,而这个容器是继承与View的。
2、ViewGroup是一个基类,并且是Layout和一些View组件的基类。
等等,不一而足,眼界有多高相信看到的就有多远,呵呵。
二、ViewGroup的三个方法
在继承ViewGroup时有三个重要的方法,下面我们就来看看:
1、onLayout方法
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
}
在我们继承ViewGroup时会在除了构造函数之外提供这个方法,我们可以看到,在ViewGroup的源代码中方法是这样定义的,也就是父类没有提供方法的内容,需要我们自己实现。
当View要为所有子对象分配大小和位置时,调用此方法
2、addView方法
public void addView(View child) {
addView(child, -1);
}
这个方法是用来想View容器中添加组件用的。我们可以使用这个方法想这个ViewGroup中添加组件。
3、getChildAt方法
public View getChildAt(int index) {
try {
return mChildren[index];
} catch (IndexOutOfBoundsException ex) {
return null;
}
}
这个方法用来返回指定位置的View。
注意:ViewGroup中的View是从0开始计数的。
可以说我们自定义ViewGroup时这三个方法是至关重要的,下面我们就来看看自定义ViewGroup使用。
三、一个小Demo
我们新建一个叫AndroidViewGroup的工程,Activity起名为MainActivity。在写一个继承于ViewGroup的类,名叫HelloViewGroup。
-->HelloViewGroup类
public class HelloViewGroup extends ViewGroup {
public HelloViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public HelloViewGroup(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
}
}
-->MainActivity类
public class MainActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new HelloViewGroup(this));
}
}
这时你可以运行一下,发现屏幕除了状态栏了那个Label是一片黑,呵呵。下面我们来修改代码,让自己的ViewGroup火起来。
我们新建一个名叫myAddView的方法,这个方法用来向ViewGroup中添加组件:
/**
* 添加View的方法
* */
public void myAddView(){
ImageView mIcon = new ImageView(mContext);
mIcon.setImageResource(R.drawable.haha);
addView(mIcon);
}
然后我们修改onLayout方法:
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
View v = getChildAt(0);
v.layout(l, t, r, b);
}
然后我们看看运行效果:
相关文章推荐
- Android 自定义ViewGroup起步(转载)
- 50个Android开发技巧(03 自定义ViewGroup)
- Android自定义ViewGroup
- Android控件架构与自定义控件详解(三)——自定义ViewGroup
- Android 手把手教您自定义ViewGroup
- Android动画效果之自定义ViewGroup添加布局动画
- android自定义viewgroup初步之一----抽屉菜单
- Android 手把手教您自定义ViewGroup
- Android自定义ViewGroup的实现方法
- Android ViewDragHelper完全解析 自定义ViewGroup神器
- Android中自定义ViewGroup
- Android 使用自定义ViewGroup实现圆形布局
- Android进阶学习总结-自定义ViewGroup和属性
- Android学习小demo(3)自定义ViewGroup 利用 scroller 实行屏幕滚动
- Android 手把手教您自定义ViewGroup(一)
- Android 手把手教您自定义ViewGroup(一)
- Android 自定义ViewGroup 实战篇 -> 实现FlowLayout
- Android ViewDragHelper完全解析 自定义ViewGroup神器
- Android自定义ViewGroup(一)之CustomGridLayout
- android 自定义ViewGroup和对view进行切图动画实现滑动菜单SlidingMenu