zoukankan      html  css  js  c++  java
  • 自己定义View-2-重写onMeasure

    效果图

    这里写图片描写叙述

    布局文件

    <?xml version="1.0" encoding="utf-8"?

    > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="100dp" android:background="#003839" android:gravity="center" android:visibility="visible" android:text="SECOND"/> <com.pengkv.apple.weight.FirstView android:layout_width="wrap_content" android:visibility="visible" android:background="#888787" android:layout_height="wrap_content"/> </LinearLayout>

    View代码

    public class FirstView extends LinearLayout {
    
        public FirstView(Context context) {//代码实例化的时候调用
            super(context);
        }
    
        public FirstView(Context context, AttributeSet attrs) {//布局文件引用的时候调用
            super(context, attrs);
        }
    
        public FirstView(Context context, AttributeSet attrs, int defStyleAttr) {//自己定义属性值的时候调用
            super(context, attrs, defStyleAttr);
        }
    
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    //        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
            setMeasuredDimension(measureSpec(widthMeasureSpec),measureSpec(heightMeasureSpec)); //重測尺寸
        }
    
        public int measureSpec(int measureSpec){
            int result=0;
            int specMode=MeasureSpec.getMode(measureSpec);//获取測量模式
            int specSize=MeasureSpec.getSize(measureSpec);//获取測量尺寸
    
            if (specMode==MeasureSpec.EXACTLY){//精确模式:包含准确设置dp值和match_parent
                result=specSize;
            }else {
                result=400;//默认设置的尺寸
                if (specMode==MeasureSpec.AT_MOST){
                    result=Math.min(result,specSize);
                }
            }
            return result;
        }
    }
  • 相关阅读:
    移动端获取屏幕的宽度,根据屏幕大小动态设置html的rem字体大小
    解析CSS3伪类选择器nth-of-type和nth-child的用法,以及两者的区别
    移动端的1px的解决方案
    Vue中import from的来源:省略后缀与加载文件夹
    flex布局
    前端开发人员快速创建本地服务器
    centos6.5Xen4.2安装
    centos6.5kvm虚拟化安装部署
    CentOS搭建svn服务器支持https访问
    CentOS6.5搭建LNMP
  • 原文地址:https://www.cnblogs.com/wzjhoutai/p/7382779.html
Copyright © 2011-2022 走看看