您的位置:首页 > 移动开发 > Android开发

Android ViewGroup学习总结-初步(Demo)

2013-05-13 10:56 519 查看
 

ViewGroup对应的api概述:   

     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
   ViewGroup.LayoutParams
class which serves as the
base class for layouts parameters.     

简单来说,ViewGroup就是一个可以装view的特殊View。说到这里大家想想看我们有哪些view是可以添加view的。比如说我们的布局文件,里面可以添加各种view,可以在xml Layout文件中添加,也可以在java代码中添加。没错,这些正是viewGroup的直接子类。像LinearLayou、RelativeLayout、Framelayout等均为其直接之类。Also see ,ViewGroup也有很多比较常用的间接子类。比如ListView啊、下拉列表Spinner啊、Datepicker/TimePicker啊、TabHost等等这些大家所比较常用的可以添加view(或者说view组合)的控件。

 

VIewgroup绘图流程:

在两个步骤中分别调用回调函数:1.onMeasure()   2.onLayout()

  1.onMeasure() 在这个函数中,ViewGroup会接受childView的请求的大小,然后通过childView的 measure(newWidthMeasureSpec, heightMeasureSpec)函数存储到childView中,以便childView的getMeasuredWidth() andgetMeasuredHeight() 的值可以被后续工作得到。

  2.onLayout() 在这个函数中,ViewGroup会拿到childView的getMeasuredWidth() andgetMeasuredHeight(),用来布局所有的childView。

  3.View.MeasureSpec与 LayoutParams 这两个类,是ViewGroup与childView协商大小用的。其中,View.MeasureSpec是ViewGroup用来部署 childView用的, LayoutParams是childView告诉ViewGroup 我需要多大的地方。

  4.在View 的onMeasure的最后要调用setMeasuredDimension()这个方法存储View的大小,这个方法决定了当前View的大小。Demo

Demo:自定义布局文件

       在学习ViewGroup的时候,先查看了文档,由于英语水平有限,看文档看得并是不是很清楚,也查了一些博客的资料,发现一个比较好的Demo,我稍稍做了修改。

想要运行该Demo、该Demo可以理解为一个布局文件(类似linearlayout布局风格),解决的问题是:当我们向空间中添加控件的时候,当空间一行放满的时候,会自动换行。这样的功能android并没有提供对应的布局文件,所以我们可以利用ViewGroup自己定义一个布局文件,来实现控件自动换行的目的。

主要包括两个类,一个activity,用来添加自己的view到自定义的viewgroup上,一个自己定义的 viewgroup。Demo效果图如下:



 

 

activity类,用来添加测试view到myviewGroup上

package test.forpip.android_views;

import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyViewGroup myViewGroup = new MyViewGroup(this);
setContentView(new MyViewGroup(this));
Button Button1 = new Button(this);
Button1.setText("中华人民共和国");
myViewGroup.addView(Button1);
Button Button2 = new Button(this);
Button2.setText("hahahaha some!");
myViewGroup.addView(Button2);
Button Button3 = new Button(this);
Button3.setText("你好!     !!");
myViewGroup.addView(Button3);
Button Button4 = new Button(this);
Button4.setText("Android ViewGroup测试");
myViewGroup.addView(Button4);
Button Button = new Button(this);
Button.setText("这是一首简单的小情歌,唱着人们心中的曲折,我想我很快乐。。");
myViewGroup.addView(Button);
Button button5 = new Button(this);
button5.setText("Java");
myViewGroup.addView(button5);
Button button6 = new Button(this);
button6.setText("Android");
myViewGroup.addView(button6);
Button button7 = new Button(this);
button7.setText("C/C++");
myViewGroup.addView(button7);
Button button8 = new Button(this);
button8.setText("HTML5");
myViewGroup.addView(button8);
setContentView(myViewGroup);
}
}

一个自定义的ViewGroup类,用来实现控件填满一行的时候自动换行。

 

package test.forpip.android_views;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

public class MyViewGroup extends ViewGroup{
private final static int VIEW_MARGIN = 2;//每个view之间的间距
public MyViewGroup(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
Log.d("MyViewGroup", "width:"+widthMeasureSpec+", height:"+heightMeasureSpec);
for (int index = 0; index < getChildCount(); index++) {
final View child = getChildAt(index);
// measure
child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
//l:left t:top r:right b:bottom
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
Log.d("MyViewGroup", changed +", left:"+l+" " +
"top:"+ " right:"+" bottom"+b);
//获取所有的view的数量
final int count = getChildCount();
int row=0;// which row lay you view relative to parent
int lengthX=l;    // right position of child relative to parent
int lengthY=t;    // bottom position of child relative to parent
for(int i=0;i<count;i++){
final View child = this.getChildAt(i);
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
lengthX+=width+VIEW_MARGIN;
lengthY=row*(height+VIEW_MARGIN)+VIEW_MARGIN+height+t;
//if it can't drawing on a same line , skip to next line
if(lengthX>r){
lengthX=width+VIEW_MARGIN+l;
row++;
lengthY=row*(height+VIEW_MARGIN)+VIEW_MARGIN+height+t;

}
child.setVisibility(View.VISIBLE);
child.layout(lengthX-width, lengthY-height, lengthX, lengthY);
}
}

}


很多东西处理的比较粗糙,主要是想体现ViewGroup的作用。并没有精细 的去定义这个布局文件。

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ViewGroup Android