首页 技术 正文
技术 2022年11月18日
0 收藏 996 点赞 4,502 浏览 6541 个字

  自定义View包括很多种,上一次随笔中的那一种是完全继承自View,这次写的这个小Demo是继承自ViewGroup的,主要是将自定义View继承自ViewGroup的这个流程来梳理一下,这次的Demo中自定义了一个布局的效果,并且这个自定义布局中包含布局自己的属性,布局中的控件也包含只属于这个布局才具有的自定义属性(类似于layout_weight只存在于LinearLayout中,只有LinearLayout中的控件可以使用一样)。话不多说,先看效果图:

自定义View(二)–继承自ViewGroup

  其中红色的部分是自定义的ViewGroup,这里使用了wrap_content,所以包裹内容。

  我的思路是这样的:

  先自定义一个类,继承自ViewGroup,继承自ViewGroup的自定义类,一定要重写onLayout()方法,因为这是一个抽象方法,主要作用就是用来绘制子View视图,确定ViewGroup中的每个子控件的位置。然后再重写其两个构造函数,一个是一个参数的,一个是两个参数的。继承自ViewGroup与继承自View有些不同的地方,我们在重写onMeasure()的时候,不仅仅要测量自定义的父布局(本自定义View)的尺寸,还要在其中计算其子View的尺寸信息,这是比较麻烦的地方,计算完尺寸还要再onLayout()方法中根据计算出来的控件的摆放位置,调用 子View.layout(int left,int top,int right,int bottom)方法将控件绘制在ViewGroup上,其中的四个参数为子View控件的左上角的点的坐标和右下角的点的坐标信息。这里我们为子View定义了两个自定义属性,实现类似margin的效果,而只要定义了子控件的这类属性,就要自定义一个内部类,继承自MarginLayoutParams类,在这里获取我们在xml中设置的属性信息。也就是主要的方法和与继承自View的自定义View中不同的就是onMeasure()方法,自定义类继承自MarginLayoutParams,onLayout()方法,generateLayoutParams()方法。还不是有点懵?来看看代码,如果是在不懂,可以把代码拷贝到工程中自己改改属性试一下,就会明白不少!

  attr.xml:

 <?xml version="1.0" encoding="utf-8"?>
<resources>
<!--声明自定义属性 自定义ViewGroup的属性 -->
<declare-styleable name="CascadeViewGroup">
<attr name="verticalSpacing" format="dimension|reference"></attr>
<attr name="horizontalSpacing" format="dimension|reference"></attr>
</declare-styleable>
<!--以下自定义属性针对自定义CascadeViewGroup布局中的子控件设置的属性-->
<declare-styleable name="CascadeViewGroup_LayoutParams">
<attr name="layout_paddingLeft" format="dimension|reference"></attr>
<attr name="layout_paddingTop" format="dimension|reference"></attr>
</declare-styleable>
</resources>

  自定义View类:

 package com.example.customviewgroup; import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup; /**
*/
public class CascadeViewGroup extends ViewGroup{
//声明自定义布局中的子控件距离父布局的间距的宽度和高度
private int mHoriztonalSpacing;
private int mVerticalSpacing; public CascadeViewGroup(Context context) {
super(context);
} public CascadeViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.CascadeViewGroup);
mHoriztonalSpacing=array.getDimensionPixelSize(
R.styleable.CascadeViewGroup_horizontalSpacing,R.dimen.default_horizontal_spacing);
mVerticalSpacing=array.getDimensionPixelSize(
R.styleable.CascadeViewGroup_verticalSpacing,R.dimen.default_vertical_spacing);
array.recycle();
} /**
* onMeasure 测量自身大小 测量子view的大小
* 并且将子view信息保存到LayoutParams中
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//获取viewgroup中子view 的个数
int count=this.getChildCount();
//获取当前光标的坐标位置 横坐标 纵坐标
int width=getPaddingLeft();
// Log.d("Tag", "base-width: "+width);
int height=this.getPaddingTop();
// Log.d("Tag", "base-height: "+height);
for(int i=0;i<count;i++){
View currentView=getChildAt(i);
//测量每个子view的宽度和高度
measureChild(currentView,widthMeasureSpec,heightMeasureSpec);
LayoutParams lp= (LayoutParams) currentView.getLayoutParams();
//判断子view是否设置自定义属性padding
if(lp.mSettingPaddingLeft!=0){
width+=lp.mSettingPaddingLeft;
}
if(lp.mSettingPaddingTop!=0){
height+=lp.mSettingPaddingTop;
}
//获取子view的起始点坐标
lp.x=width;
lp.y=height;
width+=mHoriztonalSpacing+currentView.getMeasuredWidth();
height+=mVerticalSpacing+currentView.getMeasuredHeight();
}
//因为最后一次循环多加上了一个mHoriztonalSpacing和mVerticalSpacing,
// 所以父布局的wrap_content情况的话需要减掉一个mHoriztonalSpacing和mVerticalSpacing
width+=getPaddingRight()-mHoriztonalSpacing;
height+=getPaddingBottom()-mVerticalSpacing;
//设置自定义ViewGroup的宽度和高度
//resolveSize()主要就是根据指定的尺寸大小和模式 返回需要的大小值
// 自动判断是哪一种模式,并返回需要的尺寸大小(match_parent或者是指定大小或者是wrap_content)
setMeasuredDimension(resolveSize(width,widthMeasureSpec),
resolveSize(height,heightMeasureSpec));
} /**
* 如果想在自定义ViewGroup中使用margin设置间距,则要在自定义类中重写generateLayoutParams系列方法才行
* @return
*/
@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
} @Override
public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(this.getContext(),attrs);
} @Override
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
return new LayoutParams(p);
} /**
* 以内部类的形式自定义LayoutParams 方便测量子view时保存子控件使用的属性值
* 当ViewGroup中的子控件中有自己独特的属性的时候才重写自定义一个类LayoutParams继承自MarginLayoutParams
*/
public static class LayoutParams extends MarginLayoutParams{
//记录子view的起始点
int x;
int y;
//子控件的自定义属性,这里的效果相当于margin的效果
int mSettingPaddingLeft;
int mSettingPaddingTop; public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
TypedArray array=c.obtainStyledAttributes(attrs,R.styleable.
CascadeViewGroup_LayoutParams);
mSettingPaddingLeft=array.getDimensionPixelSize(R.styleable.
CascadeViewGroup_LayoutParams_layout_paddingLeft,0);
mSettingPaddingTop=array.getDimensionPixelSize(
R.styleable.CascadeViewGroup_LayoutParams_layout_paddingTop,0);
array.recycle();
} public LayoutParams(int width, int height) {
super(width, height);
} public LayoutParams(MarginLayoutParams source) {
super(source);
} public LayoutParams(ViewGroup.LayoutParams source) {
super(source);
}
} /**
* 确定布局子view的位置
*/
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int count=getChildCount();
for(int i=0;i<count;i++){
View currentView=getChildAt(i);
LayoutParams lp= (LayoutParams) currentView.getLayoutParams();
currentView.layout(lp.x,lp.y,lp.x+currentView.getMeasuredWidth(),
lp.y+currentView.getMeasuredHeight());
}
}
}

  布局文件:

 <?xml version="1.0" encoding="utf-8"?>
<com.example.customviewgroup.CascadeViewGroup
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
app:horizontalSpacing="10dp"
app:verticalSpacing="10dp"
> <!--<TextView-->
<!--android:layout_width="100dp"-->
<!--android:layout_height="wrap_content"-->
<!--android:background="#ff0000"-->
<!--android:textSize="20sp"-->
<!--android:text="打发打发斯蒂芬" />--> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00ff00"
android:textSize="20sp"
android:text="Hello World!" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00ffff"
android:textSize="20sp"
android:text="Hello World!" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0000ff"
android:textSize="20sp"
android:text="Hello World!" />
</com.example.customviewgroup.CascadeViewGroup>

  基本上就这么多,但是会有一个小Bug,目前还不太清楚是怎么回事,就是在布局文件的根布局下如果不指定我们自定义的app:horizontalSpacing=”10dp”和app:verticalSpacing=”10dp”这两个属性,在自定义View的第40行和第42行getPaddingLeft()和getPaddingTop()的时候会得到一个非常大的值,即使我们指定了padding的值也是这样,也就是说必须设定这两个自定义属性,我们设置的默认padding没有起作用。不想有间距的话可以设成0dp,这个坑我找到解决办法之后再来填。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,087
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,562
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,821
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905