您的位置:首页 > 其它

继承ViewGroup研究(汇总) 一、二、三

2013-08-02 11:48 225 查看


转载过来:为一、二、三版本。

仅供参考:




继承ViewGroup研究(1)
--简介和一个小Demo

又翻开一个新篇章了,哈哈,上一回学习的是继承View,关于继承View个人感觉不是那么完美,做技术的总是想让一切完美,但世界上本就没有完美,由他吧。这回研究的是ViewGroup。说实话,个人感觉这个类的功能还是很强大的,这里我们只给出最基本的东西,好了,继续开始研究吧,,路漫漫其修远兮,吾将上下而求索。

一、ViewGroup概述

研究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组件的基类,这个类还定义了LayoutParams类来指定这个基类的布局参数。(翻译的不太好,能看懂就行了)

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的源代码中方法是这样定义的,也就是父类没有提供方法的内容,需要我们自己实现。

这个是我们布局的时候用的,待会我们就会使用到这个。

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);

}

然后我们看看运行效果:

是不是出效果了,哈哈,自己试一试吧,不过是之前记得创建一个mContext并在构造函数里初始化一下。

继承ViewGroup研究(2)--在XML中使用ViewGroup初探

这次研究的是XML中布局自定义ViewGroup,为什么要在XML中自定义ViewGroup,呵呵,其实这个东西吧,还真没法说,我个人的感觉是可以方便的使用Android中已经给出的各种组件,还有自己的组件。下面我们就来看看在XML中布局。

一、XML中使用ViewGroup概述

首先我们要知道Android中的View以及ViewGroup,甚至是自己继承Android中系统组件的View,都可以在XML中使用。不过要注意的一点就是一定要在自定义的类中添加上以下的构造器:

public MyView(Context context, AttributeSet attrs) {

super(context, attrs);

// TODO Auto-generated constructor stub

}

同样,在XML中定义ViewGroup也必须有这个方法。因为从Android系统解析自定义的View时是根据这个方法来解析的,没有这个方法是不行的。各位可以试一试。

二、在XML中使用

我们先来修改MainActivity类

public class MainActivity extends Activity {

/** Called when the activity is first created. */

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

}

修改main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

<com.fxhy.stady.myviewGroup.HelloViewGroup

android:layout_width="fill_parent"

android:layout_height="fill_parent"/>

</LinearLayout>

我们运行一下: 图省略

完美运行,哈哈,有没点成就感啊,哈哈。

三、关于三种在XML使用ViewGroup

其实我们有三种在XML布局中使用ViewGroup的方法:

1、直接使用ViewGroup

这个就是我们刚才使用的那个方法,这个方法是在自己写的ViewGroup中通过addView方法一个一个添加子View,个人感觉这样使用不太爽,呵呵,但是不可否认这样使用有其独挡一面的地方。

2、在XML中向ViewGroup中添加View。

这个方法我们将在下一节研究,关于这个的话会涉及到一些其他的知识点,我们现在先不探讨这个。

3、混合布局

就像css+table一样,呵呵,我们是不是可以既使用XML中声明的ViewGroup中添加View,又在自己写的类中添加View???那这样添加的View是先添加XML中的,还是先添加在自定义的类中的???

呵呵,这个一个需要探究的问题,有兴趣的可以先自己研究下,在以后的章节中我会给出研究结果。

未完待续......

继承ViewGroup研究(3)--在XML向ViewGroup中添加组件

这节我们研究的是在XML中向ViewGroup中添加组件。关于这个的研究可以破费周折,为什么呢??因为向其中添加的组件他都不显示啊。郁闷了好长一段时间,不多最终还是突破了,研究出来了,呵呵,路漫漫其修远兮,吾将上下而求索。继续我们的探索之旅。

一、简述

在XML中向ViewGroup中添加组件,这个问题貌似很简单,但是只有在简单中才会发现不简单,其实好多复杂的东西都是由简单的东西一步一步演化而来的,这是道家所谓的一生二、二生三、三生万物,以后我们还将通过剖析一个布局类来更深入的了解ViewGroup,不过那是后话了,呵呵。

那么我们可以通过怎样的方式来添加了。请看:

<com.fxhy.stady.myviewGroup.HelloViewGroup

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TextView

android:layout_height="wrap_content"

android:layout_width="fill_parent"

android:text="繁星皓月继承ViewGroup研究"/>

</com.fxhy.stady.myviewGroup.HelloViewGroup>

貌似这个方法可以先ViewGroup中加入组件,但是,实际上我们运行后会发现,其实这个组件是不会显示的。

有兴趣的读者可以运行试试。效果是下面这样的。



二、为啥不显示

那么为啥这个ViewGroup里面的TextView没有显示呢,我也为这个问题困扰过很久,其实,有时候我们不是学不会,而是不会学,知识千万遍,方法有多少。那到底要怎样做呢?

其实很简单,我们只需要在ViewGroup中的onMeasure方法里添加一个对子元素的遍历,并且在onLayout中添加一个布局遍历就实现了简单的布局了。

下面给出代码:

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{

// TODO Auto-generated method stub

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int childCount = getChildCount();

for(int i = 0; i < childCount; i ++){

View v = getChildAt(i);

v.measure(widthMeasureSpec, heightMeasureSpec);

}

}

@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 v = getChildAt(i);

v.layout(l, t, r, b);

}

}

运行效果:



三、注意

各位读者请注意:

以上的方法仅仅是一个实验,实际上布局要比上述的复杂的多,有兴趣的可以看看LinearLayout里面是怎样实现的来研究下,以后如果有时间我会写一篇浅析Android布局的博文,供大家参考。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: