在发展中,有时会遇到一些要求。布局和控制系统不仅提供使用,以满足我们的发展,所以这一次就行,通常是你自己的自定义布局(ViewGroup)并控制(View)该。我在这里,我们将用一个简单的例子,当他们解释他们的定义ViewGroup基本流程,我希望能帮助朋友不理解这个过程。

首先,我们想要实现的布局图例如以下:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd3V6aGlwZW5nMTk5MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

就这样看起来十分简单,用系统提供的布局就能够实现了这个效果,根本不须要自己定义ViewGroup来实现嘛!!

。只是,假设你自己去尝试一下用系统的布局来做。你就会发现一些问题了。问题1是:要实现各个view错开的效果,就必须为他们设置不同的固定外边距參数。这样可能带来的问题就是:不同手机。显示效果可能会不一样。也就是适配问题!问题2是:假设想要增加的View多了,还须要自己计算每一个View的外边距參数。非常坑爹,再说,这里主要是解说自己定义ViewGroup的基本流程,所以,样例越简单,也就越好理解了!

首先,我们能够自己定义自己ViewGroup想要的属性;这里我为ViewGroup定义了两个属性,horizontal_spacing(布局中的view的水平间距)和vertical_spacing(布局中的View的垂直间距);另外,还定义了一个布局參数的属性,layout_vertical_spacing。这个属性能够供我们自己定义的ViewGroup中的View使用。

attrs.xml 的内容:

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

>
<resources> <declare-styleable name="MyCustomLayout">
<attr name="horizontal_spacing" format="dimension" />
<attr name="vertical_spacing" format="dimension" />
</declare-styleable> <declare-styleable name="MyCustomLayout_LayoutParams">
<attr name="layout_vertical_spacing" format="dimension" />
</declare-styleable> </resources>

在dimens.xml中定义两个属性的默认值。

dimens.xml 的内容:

<?

xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="horizontal_spacing">10dp</dimen>
<dimen name="vertical_spacing">10dp</dimen>
</resources>

接着,最基本的步骤来了。先看看我们自己定义的ViewGroup。

以下是源代码:

package com.customlayout.mycustomlayout;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup; public class MyCustomLayout extends ViewGroup { private int mHorizontalSpacing;
private int mVerticalSpacing; public MyCustomLayout(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyCustomLayout);
mHorizontalSpacing = ta.getDimensionPixelSize(R.styleable.MyCustomLayout_horizontal_spacing,
getResources().getDimensionPixelSize(R.dimen.horizontal_spacing));
mVerticalSpacing = ta.getDimensionPixelSize(R.styleable.MyCustomLayout_vertical_spacing,
getResources().getDimensionPixelSize(R.dimen.vertical_spacing));
ta.recycle();
} @Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
// TODO Auto-generated method stub
return p != null;
} @Override
protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
// TODO Auto-generated method stub
return (LayoutParams) p;
} @Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
} @Override
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = 0;
int height = getPaddingTop();
int verticalSpacing;
final int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
measureChild(child, widthMeasureSpec, heightMeasureSpec);
verticalSpacing = mVerticalSpacing;
LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (lp.verticalSpacing > 0) {
verticalSpacing += lp.verticalSpacing;
}
width = getPaddingLeft() + mHorizontalSpacing * i;
lp.x = width;
lp.y = height;
width += child.getMeasuredWidth();
height += verticalSpacing;
}
width += getPaddingRight();
height += getChildAt(getChildCount() - 1).getMeasuredHeight() + getPaddingBottom(); setMeasuredDimension(resolveSize(width, widthMeasureSpec),
resolveSize(height, heightMeasureSpec));
} @Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
LayoutParams lp = (LayoutParams) child.getLayoutParams();
child.layout(lp.x, lp.y, lp.x + child.getMeasuredWidth(), lp.y + child.getMeasuredHeight());
}
} public static class LayoutParams extends ViewGroup.LayoutParams {
public int x;
public int y;
public int verticalSpacing; public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
TypedArray ta = c.obtainStyledAttributes(attrs, R.styleable.MyCustomLayout_LayoutParams);
verticalSpacing = ta.getDimensionPixelSize(R.styleable.MyCustomLayout_LayoutParams_layout_vertical_spacing, -1);
ta.recycle();
} public LayoutParams(int w, int h) {
super(w, h);
}
}
}

首先,我们从MyCustomLayout.java 的构造方法说起。

public MyCustomLayout(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyCustomLayout);
mHorizontalSpacing = ta.getDimensionPixelSize(R.styleable.MyCustomLayout_horizontal_spacing,
getResources().getDimensionPixelSize(R.dimen.horizontal_spacing));
mVerticalSpacing = ta.getDimensionPixelSize(R.styleable.MyCustomLayout_vertical_spacing,
getResources().getDimensionPixelSize(R.dimen.vertical_spacing));
ta.recycle();
}

这个构造方法有两个參数,当中attrs是我们布局的属性集合,有了这个參数。我们就能够获取到在布局文件xml中设置的相关属性了。

接着,讲onMeasure方法。顾名思义。这种方法是一个測量方法。它的作用是:遍历布局中的每个View。对每个View进行測量(调用measureChild方法),接着再为View设置LayoutParams。设置View的位置,即在屏幕上的x。y坐标。以便供onLayout方法中使用。这里的LayoutParams是我们重写的,当中int x和int y保存了布局中View的坐标位置。注意:重写LayoutParams类时。必需要对ViewGroup中的checkLayoutParams(ViewGroup.LayoutParams
p)、generateLayoutParams(ViewGroup.LayoutParams p)、generateLayoutParams(AttributeSet attrs)、generateDefaultLayoutParams()进行重写,否则将会出现异常。。。

接下来。讲onLayout方法;这种方法是对布局中的所有View进行位置部署。从方法体中能够看到,它通过一个遍历,对布局中的每个View调用layout方法进行位置部署。

好了,在这里略微总结下:自己定义ViewGroup流程中。 最主要是对onMeasure和onLayout两个方法进行重写。onMeasure通过遍历布局中的View。为每个View測量了大小、计算布局參数等。onLayout则是通过遍历布局中的View,为每个View进行位置布置。

在activity_main.xml 中使用我们自己定义的ViewGroup:

<com.customlayout.mycustomlayout.MyCustomLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:customLayout="http://schemas.android.com/apk/res-auto"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
customLayout:horizontal_spacing="65dp"
customLayout:vertical_spacing="85dp"> <View
android:layout_width="100dp"
android:layout_height="150dp"
android:background="#00ff00" /> <View
android:layout_width="100dp"
android:layout_height="150dp"
android:background="#0000ff" /> <View
android:layout_width="100dp"
android:layout_height="150dp"
android:background="#ff0000" /> <View
android:layout_width="100dp"
android:layout_height="150dp"
android:background="#0ff000" /> <View
android:layout_width="100dp"
android:layout_height="150dp"
android:background="#00f0f0" /> </com.customlayout.mycustomlayout.MyCustomLayout>

MainActivity.java 的源代码:

package com.customlayout.mycustomlayout;

import android.app.Activity;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.BounceInterpolator;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation; public class MainActivity extends Activity { MyCustomLayout layout;
LayoutAnimationController layoutAnimationController;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); layout = (MyCustomLayout) findViewById(R.id.layout); AnimationSet set = new AnimationSet(true); AlphaAnimation a = new AlphaAnimation(0f, 1f);
a.setDuration(500);
TranslateAnimation t = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
t.setDuration(500); set.addAnimation(t);
set.addAnimation(a);
layoutAnimationController = new LayoutAnimationController(set);
layout.setLayoutAnimation(layoutAnimationController); }
}

最后。讲一下布局动画。在ViewGroup类中,有一个属性是LayoutAnimation,也就是所谓的布局动画。仅仅要我们指定一个动画给这个属性,那么ViewGroup中的每个在布局时都可以带有动画效果。在上面的onCreate()方法中。我指定了一个动画集合并设置给了我自己定义好的MyCustomLayout。

执行一下程序,在网上看到。MyCustomLayout每View将在顺序和我的电影了,现在被指定MyCustomLayout在,其效果是非常酷!

结合这个简单的例子,给你自己定义的简单介绍ViewGroup方法;我们使用其他复杂的布局,所有使用我上面描述将实施的方法。他们的计算将仅比许多上述例子更复杂,但基本原理仍然是。

如何定义自己的ViewGroup的更多相关文章

  1. 定义你自己ViewGroup

    尊重原创:http://blog.csdn.net/yuanzeyao/article/details/40264433 好久都没有写文章了,如今利用周末的时间对一些知识进行总结.便于加深理解,今天我 ...

  2. Android自己定义组件系列【1】——自己定义View及ViewGroup

    View类是ViewGroup的父类,ViewGroup具有View的全部特性.ViewGroup主要用来充当View的容器.将当中的View作为自己孩子,并对其进行管理.当然孩子也能够是ViewGr ...

  3. Android自己定义组件系列【2】——Scroller类

    在上一篇中介绍了View类的scrollTo和scrollBy两个方法,对这两个方法不太了解的朋友能够先看<自己定义View及ViewGroup> scrollTo和scrollBy尽管实 ...

  4. 转战WebApp: 最适合Android开发者的WebApp框架

    随着移动端设备越来越多, 微信应用号即将发布, 越来越多的页面需要被移动浏览器承载, HTML5开发大热, 我们需要掌握Web开发的技能来适应时代变化. 合适的WebApp框架 AndroidUI4W ...

  5. Android Touch系统简介(二):实例详解onInterceptTouchEvent与onTouchEvent的调用过程

    上一篇文章主要讲述了Android的TouchEvent的分发过程,其中有两个重要的函数:onInterceptTouchEvent和onTouchEvent,这两个函数可被重装以完成特定的逻辑.on ...

  6. Android Touch系统简介(二):实例详解onInterceptTouchEvent与onTouchEvent的调用过程

    上一篇文章主要讲述了Android的TouchEvent的分发过程,其中有两个重要的函数:onInterceptTouchEvent和onTouchEvent,这两个函数可被重装以完成特定的逻辑.on ...

  7. View事件传递之父View和子View之间的那点事

    Android事件传递流程在网上可以找到很多资料,FrameWork层输入事件和消费事件,可以参考: Touch事件派发过程详解 这篇blog阐述了底层是如何处理屏幕输,并往上传递的.Touch事件传 ...

  8. 手势滑动结束 Activity(一)基本功能的实现

    喜欢听音乐的朋友可能都看过天天动听这款 app, 这款 app 有一个亮点就是在切换页面(Fragment)的时候能够通过手势滑动来结束当前页面.这里先说一下,我为什么会这么关心这个功能呢,由于前两天 ...

  9. Android应用Activity、Dialog、PopWindow、Toast窗体加入机制及源代码分析

    [工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处.尊重劳动成果] 1 背景 之所以写这一篇博客的原因是由于之前有写过一篇<Android应用setCont ...

随机推荐

  1. 【Android先进】查看手机记忆库状态和应用方法

    一世 我们知道.android程序存储器通常被限制16M.当然,24M的,和android程序存储器分为2部分:native和dalvik.dalvik 就是我们寻常说的java堆.我们创建的对象是在 ...

  2. 怎么确定你的CPU是否支持64位虚拟化

    http://www.grc.com/securable.htm 第一个64位表示你的电脑最多支持多少位的系统,32或者64. 第二个表示你的硬件是否支持DEP?Yes,支持.No,不支持.OFF,表 ...

  3. 关于委托:异常{ 无法将 匿名方法 转换为类型“System.Delegate”,因为它不是委托类型 }

    异常{ 无法将 匿名方法 转换为类型"System.Delegate",因为它不是委托类型 } 委托实际上是把方法名作为参数,但是若有好多个方法时,就要指明是哪个参数  查看如下代 ...

  4. 两个文件中的配置项设置方法和C比较程序处理

    在实际的软件开发项目.程序经常需要翻阅了一些资料可能会改变从外部,我们需要读出的信息到一个统一的文件(一般ini档),而此文件被称为个人资料. 考虑这样一个场景,程序须要与多个数据库打交道,要从配置文 ...

  5. 采用truelicense进行Java规划license控制 扩展可以验证后,license 开始结束日期,验证绑定一个给定的mac住址

    采用truelicense进行Java规划license控制 扩展可以验证后,license 开始结束日期,验证绑定一个给定的mac住址. Truelicense 它是一个开源java license ...

  6. Android 它们的定义ContentProvider和ContentObserver充分利用

    在他们的定义ContentProvider结合ContentObserver一起使用时,自己写的ContentProvider,在运行完insert.delete和update后,要手动地调用getC ...

  7. 认识mongoDB数据库

    mongodb中有三元素:数据库,集合,文档,其中“集合”对应关系型数据库中的“表”,“文档”对应“行”. 安装mongoDB: 去官网下载对应系统的mongoDB压缩包,解压后将文件夹重命名为mon ...

  8. ABP-N层架构

    ABP理论学习之N层架构   返回总目录 自从写这个系列博客之后,发现很多园友还是希望有个直接运行的demo,其实在github上就有官方的demo,我直接把这demo的链接放到这里吧,另外,我分析, ...

  9. System.Threading.ThreadStateException

    异常:"System.Threading.ThreadStateException"在未处理的异常类型 System.Windows.Forms.dll 发生 其它信息: 在能够调 ...

  10. main thread starting…

    例的结果,下面的: main thread starting- Thrad 2 staring- Thrad 2 end- Thrad 4 staring- Thrad 4 end- Thrad 1 ...