自定义ViewGroup与view 的基本用法与特性

来源:互联网 发布:淘宝站外营销 编辑:程序博客网 时间:2024/06/06 18:18
自定义ViewGroup中,有一个特性,就是,在创建了一个容器后,里面的子控件默认是不可见的,这时候需要手动来为子控件进行显示
package one.bw.com.huizhi.view2;import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.view.ViewGroup;import android.widget.Toast;/** * Created by Administrator on 2017/11/30/030. */public class MyLayoutView extends ViewGroup {    public MyLayoutView(Context context, AttributeSet attrs) {        super(context, attrs);    }    /**     * 当前控件的宽高     * @param widthMeasureSpec     * @param heightMeasureSpec     */    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        //获得子控件        View childAt = getChildAt(0);        //测量子控件        measureChild(childAt,widthMeasureSpec,heightMeasureSpec);    }    //这里是父控件定位子控件在父控件中的位置...布局(补得是子控件)    @Override    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {        //获得所有子控件        int childCount = getChildCount();        if(childCount>0){            View childAt = getChildAt(0);            //为子空间设置大小,宽高            childAt.layout(30,30,150,150);            Toast.makeText(getContext(),childAt.getMeasuredWidth()+"",Toast.LENGTH_SHORT).show();        }    }}
//布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="one.bw.com.huizhi.Main2Activity"    android:orientation="vertical"    >    <one.bw.com.huizhi.view2.MyLayoutView        android:layout_width="300dp"        android:layout_height="300dp"        android:background="#F89"        android:layout_marginTop="50dp"        >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="123456"            android:background="#00F000"            android:id="@+id/tt"/>    </one.bw.com.huizhi.view2.MyLayoutView></LinearLayout>
//自定义view的一些特性
 private int cx = 100;    private int cy = 100;    /**     * Canvas canvas画布.....paint画笔     * @param canvas     */    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        //中心点x坐标,y坐标,半径,画笔        Paint paint = new Paint();        paint.setColor(Color.RED);//设置颜色        paint.setStrokeWidth(2);//描边的宽度2个像素        //Paint.Style.FILL填充,,Paint.Style.STROKE描边 ,,Paint.Style.FILL_AND_STROKE填充并且描边        paint.setStyle(Paint.Style.FILL);        paint.setAntiAlias(true);//设置抗锯齿        canvas.drawCircle(cx,cy,100,paint);    }
//测量控件的宽高与大小

@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    super.onMeasure(widthMeasureSpec, heightMeasureSpec);    int mode = MeasureSpec.getMode(widthMeasureSpec);    int size = MeasureSpec.getSize(widthMeasureSpec);    if (mode == MeasureSpec.EXACTLY){        //300dp......900px        //xxhdpi 1dp = 3px xhdpi 1dp = 2px hdpi 1dp = 1.5px        Toast.makeText(getContext(),"size:"+size,Toast.LENGTH_SHORT).show();    }    //onMeasure方法中直接设置大小.....在代码里面宽度和高度都是使用像素值    //只是初步指定一个大小,最终是由这个方法确定的,如果不去设置就用最开始指定的大小    setMeasuredDimension(100,100);}