自定义View需注意问题

1 ViewGroup类中包含两个静态内部类:LayoutParams 和MarginLayoutParams

public abstract class ViewGroup extends View implements ViewParent, ViewManager {
            ...省略
public static class LayoutParams {
            ...省略
}
public static class MarginLayoutParams extends   ViewGroup.LayoutParams {
             ...省略
 }
             ...省略
 }

这两个布局属性是提供给被它所包含的子View使用的。如
layout_width,layout_height等等。自定义ViewGroup如果要提供自定义的布局需要继承MarginLayoutParams 来重写其中的布局方法,比如LinearLayout ,如下

    <LinearLayout  
        android:layout_width="150dp"  
        android:layout_height="150dp" >          
        <!-- 使用自定义view -->  
        <com.stone.view.CustomView1  
            android:layout_width="wrap_content"  
            android:layout_height="fill_parent"  
            custom:score="60%"  
            custom:rotation="-45"  
            custom:color="#3f00ff00" />  
    </LinearLayout>  

上述自定义View中的布局参数(6,7两行)是包裹他的布局所定义的,如果自定义View被一个自定义的ViewGroup包裹,那么ViewGroup需要定义自己的布局参数。
LinearLayout的布局类源码如下:


    public static class LayoutParams extends ViewGroup.MarginLayoutParams {

        @ViewDebug.ExportedProperty(category = "layout")
        public float weight;

        /**
         * Gravity for the view associated with these LayoutParams.
         *
         * @see android.view.Gravity
         */
        @ViewDebug.ExportedProperty(category = "layout", mapping = {
            @ViewDebug.IntToString(from =  -1,                       to = "NONE"),
            @ViewDebug.IntToString(from = Gravity.NO_GRAVITY,        to = "NONE"),
            @ViewDebug.IntToString(from = Gravity.TOP,               to = "TOP"),
            @ViewDebug.IntToString(from = Gravity.BOTTOM,            to = "BOTTOM"),
            @ViewDebug.IntToString(from = Gravity.LEFT,              to = "LEFT"),
            @ViewDebug.IntToString(from = Gravity.RIGHT,             to = "RIGHT"),
            @ViewDebug.IntToString(from = Gravity.START,            to = "START"),
            @ViewDebug.IntToString(from = Gravity.END,             to = "END"),
            @ViewDebug.IntToString(from = Gravity.CENTER_VERTICAL,   to = "CENTER_VERTICAL"),
            @ViewDebug.IntToString(from = Gravity.FILL_VERTICAL,     to = "FILL_VERTICAL"),
            @ViewDebug.IntToString(from = Gravity.CENTER_HORIZONTAL, to = "CENTER_HORIZONTAL"),
            @ViewDebug.IntToString(from = Gravity.FILL_HORIZONTAL,   to = "FILL_HORIZONTAL"),
            @ViewDebug.IntToString(from = Gravity.CENTER,            to = "CENTER"),
            @ViewDebug.IntToString(from = Gravity.FILL,              to = "FILL")
        })
        public int gravity = -1;

        /**
         * {@inheritDoc}
         */
        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
            TypedArray a =
                    c.obtainStyledAttributes(attrs, com.android.internal.R.styleable.LinearLayout_Layout);

            weight = a.getFloat(com.android.internal.R.styleable.LinearLayout_Layout_layout_weight, 0);
            gravity = a.getInt(com.android.internal.R.styleable.LinearLayout_Layout_layout_gravity, -1);

            a.recycle();
        }

        /**
         * {@inheritDoc}
         */
        public LayoutParams(int width, int height) {
            super(width, height);
            weight = 0;
        }


        public LayoutParams(int width, int height, float weight) {
            super(width, height);
            this.weight = weight;
        }

        /**
         * {@inheritDoc}
         */
        public LayoutParams(ViewGroup.LayoutParams p) {
            super(p);
        }

        /**
         * {@inheritDoc}
         */
        public LayoutParams(ViewGroup.MarginLayoutParams source) {
            super(source);
        }


        public LayoutParams(LayoutParams source) {
            super(source);

            this.weight = source.weight;
            this.gravity = source.gravity;
        }

        @Override
        public String debug(String output) {
            return output + "LinearLayout.LayoutParams={width=" + sizeToString(width) +
                    ", height=" + sizeToString(height) + " weight=" + weight +  "}";
        }
    }
public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource {
  ...
  /**
   * The layout parameters associated with this view and used by the parent
   * {@link android.view.ViewGroup} to determine how this view should be
   * laid out.
   * {@hide}
   */
  //每个View均拥有LayoutParams属性,View添加到ViewGroup时的布局参数
  protected ViewGroup.LayoutParams mLayoutParams;  

    public void setLayoutParams(ViewGroup.LayoutParams params) {
        if (params == null) {
            throw new NullPointerException("Layout parameters cannot be null");
        }
        mLayoutParams = params;
        resolveLayoutParams();
        if (mParent instanceof ViewGroup) {
            ((ViewGroup) mParent).onSetLayoutParams(this, params);
        }
        requestLayout();
    }
  ...
}

2 invalidate(),requestLayout()以及requestFocus()均为View类中的方法,这三个方法最终会调用到ViewRoot中的schedulTraversale()方法,该函数然后发起一个异步消息,消息处理中调用performTraverser()方法对整个View进行遍历。
参考Android中View绘制流程以及invalidate()等相关方法分析

3 layout_weight属性详解(源码解读)
android中layout_weight的理解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android自定义View可以通过以下步骤实现: 1. 创建一个继承自View或其子类的类,例如继承自View的MyView类。 2. 在该类的构造函数中初始化一些属性,例如: ``` public MyView(Context context) { super(context); //初始化一些属性 mPaint = new Paint(); mPaint.setColor(Color.RED); mPaint.setStyle(Paint.Style.FILL); } ``` 3. 重写该类的onMeasure方法,以便在该View被加入到View hierarchy时能正确地测量它的大小,例如: ``` @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { //测量宽度 int width = MeasureSpec.getSize(widthMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); if (widthMode == MeasureSpec.EXACTLY) { mWidth = width; } else { mWidth = DEFAULT_WIDTH; } //测量高度 int height = MeasureSpec.getSize(heightMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); if (heightMode == MeasureSpec.EXACTLY) { mHeight = height; } else { mHeight = DEFAULT_HEIGHT; } setMeasuredDimension(mWidth, mHeight); } ``` 4. 重写该类的onDraw方法,以便在该View被绘制时能够绘制一些内容,例如: ``` @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //绘制一个矩形 canvas.drawRect(0, 0, mWidth, mHeight, mPaint); } ``` 5. 在布局文件中使用该自定义View,例如: ``` <com.example.myview.MyView android:id="@+id/myView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> ``` 注意:以上仅为自定义View的基本步骤,具体实现方式可能根据需求有所不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值