红橙Darren Android视频笔记 自定义view的三个构造方法以及一种实现换肤的方案 测量mode

红橙Darren Android视频笔记

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import static android.view.View.MeasureSpec.AT_MOST;
import static android.view.View.MeasureSpec.EXACTLY;
import static android.view.View.MeasureSpec.UNSPECIFIED;

public class MyTextView extends View {
    /**
     * 在new对象时调用
     *
     * MyTextView myTextView = new MyTextView(context);
     */
    public MyTextView(Context context) {
        super(context);
    }

    /**
     * 在xml中使用自定义view时调用
     *
     *     <com.xx.xx.xx.xxx.xxx.MyTextView
     *         android:layout_width="wrap_content"
     *         android:layout_height="wrap_content"
     *         />
     */
    public MyTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 指定style时调用 可以用于换肤
     *
     * 1.定义style
     *     <style name="defaultTextView">
     *         <item name="android:layout_width">wrap_content</item>
     *         <item name="android:layout_height">wrap_content</item>
     *         <item name="android:textColor">@color/day_dark</item>
     *     </style>
     * 2.使用style时会调用该方法
     *     <com.telenav.arp.ev.filters.connectortype.MyTextView
     *         style="@style/defaultTextView"/>
     * 3.使用此方法 在代码中监测day night或指定皮肤变化 切换style 实现换肤
     */
    public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //MeasureSpec包含了两个信息 他是一个三十二位的值 开头两位代表mode 后面30位代表值(size)
        //mode和值组合起来就是32位的MeasureSpec
        int measureMode1 = UNSPECIFIED;//开发中不常使用 利用该参数的原理可以解决ScrollView嵌套ListView的高度问题
        int measureMode2 = EXACTLY;//指定大小或match_parent fill_parent
        int measureMode3 = AT_MOST;//wrap_content
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值