标签:意义 java 测量 apple xml文件 根据 size change pixel
1.在res/values/styles.xml
文件里面声明一个我们自定义的属性:
<resources> <!--name为声明的"属性集合"名,可以随便取,但是最好是设置为跟我们的View一样的名称--> <declare-styleable name="CircleView"> <!--声明我们的属性,名称为default_size,取值类型为尺寸类型(dp,px等)--> <attr name="radius" format="dimension"></attr> <attr name="circle_color" format="color"></attr> </declare-styleable> </resources>
public CircleView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); //即属性集合的标签,在R文件中名称为R.styleable+name TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleView); //第一个参数为属性集合里面的属性,R文件名称:R.styleable+属性集合名称+下划线+属性名称 //第二个参数为,如果没有设置这个属性,则设置的默认的值 radius = typedArray.getDimensionPixelOffset(R.styleable.CircleView_radius, 20); color = typedArray.getColor(R.styleable.CircleView_circle_color, 000000); //最后记得将TypedArray对象回收 typedArray.recycle(); }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:rc="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.ruanchao.todaynews.UserViewActivity"> <com.ruanchao.todaynews.view.CircleView android:layout_width="wrap_content" android:layout_height="wrap_content" rc:radius="50dp" rc:circle_color="#FF69B4"/> </LinearLayout>
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
widthMeasureSpec
和heightMeasureSpec包含了两层信息:测量模式和测量尺寸
(1)测量模式:
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
测量模式 | 表示意思 |
---|---|
UNSPECIFIED | 父容器没有对当前View有任何限制,当前View可以任意取尺寸(match_parent) |
EXACTLY | 当前的尺寸就是当前View应该取的尺寸(xml配置固定值) |
AT_MOST | 当前尺寸是当前View能取的最大尺寸(wrap_content) |
(2)尺寸大小:
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
private int getMySize(int defaultSize, int measureSpec) { int mySize = defaultSize; int mode = MeasureSpec.getMode(measureSpec); int size = MeasureSpec.getSize(measureSpec); switch (mode) { case MeasureSpec.UNSPECIFIED: {//如果没有指定大小,就设置为默认大小 mySize = defaultSize; break; } case MeasureSpec.AT_MOST: {//如果测量模式是最大取值为size //我们将大小取最大值,你也可以取其他值 mySize = size; break; } case MeasureSpec.EXACTLY: {//如果是固定的大小,那就不要去改变它 mySize = size; break; } } return mySize; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int width = getMySize(100, widthMeasureSpec); int height = getMySize(100, heightMeasureSpec); if (width < height) { height = width; } else { width = height; } setMeasuredDimension(width, height); }
<com.hc.studyview.MyView android:layout_width="match_parent" android:layout_height="100dp" android:background="#ff0000" />
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int with = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); int withMode = MeasureSpec.getMode(widthMeasureSpec); int heigthMode = MeasureSpec.getMode(heightMeasureSpec); if (getChildCount() == 0) {//如果没有子View,当前ViewGroup没有存在的意义,不用占用空间 setMeasuredDimension(0, 0); return; } if (withMode == MeasureSpec.AT_MOST && heigthMode == MeasureSpec.AT_MOST){ //高度累加,宽度取最大 setMeasuredDimension(getMaxChildWidth(),getTotleHeight()); }else if (heigthMode == MeasureSpec.AT_MOST){ setMeasuredDimension(with,getTotleHeight()); }else if (withMode == MeasureSpec.AT_MOST){ setMeasuredDimension(getMaxChildWidth(),height); } } /*** * 获取子View中宽度最大的值 */ private int getMaxChildWidth() { int childCount = getChildCount(); int maxWidth = 0; for (int i = 0; i < childCount; i++) { View childView = getChildAt(i); if (childView.getMeasuredWidth() > maxWidth) { maxWidth = childView.getMeasuredWidth(); } } return maxWidth; } /*** * 将所有子View的高度相加 **/ private int getTotleHeight() { int childCount = getChildCount(); int height = 0; for (int i = 0; i < childCount; i++) { View childView = getChildAt(i); height += childView.getMeasuredHeight(); } return height; }
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int count = getChildCount(); int currentHeigth = 0; //将子View逐个摆放 for (int i = 0; i < count; i++) { View child = getChildAt(i); int childHeigth = child.getMeasuredHeight(); child.layout(left, currentHeigth, right + child.getMeasuredWidth(), currentHeigth + childHeigth); currentHeigth += childHeigth; } }
<com.rc.studyview.MyViewGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ff9900"> <Button android:layout_width="100dp" android:layout_height="wrap_content" android:text="btn" /> <Button android:layout_width="200dp" android:layout_height="wrap_content" android:text="btn" /> <Button android:layout_width="50dp" android:layout_height="wrap_content" android:text="btn" /> </com.rc.studyview.MyViewGroup>
Android 自定义View、ViewGroup和自定义属性
标签:意义 java 测量 apple xml文件 根据 size change pixel
原文地址:http://www.cnblogs.com/xiakexinghouzi/p/7843409.html