Android自定义View——可以设置最大宽高的FrameLayout
2018-06-08 14:04
731 查看
可以设置最大宽高的FrameLayout
支持相对父控件的半分比设置
默认优先比例设置
不支持参数小于零
MaxLayout.java
attrs.xml
支持相对父控件的半分比设置
默认优先比例设置
不支持参数小于零
MaxLayout.java
import android.util.DisplayMetrics; import android.view.ViewGroup; import android.widget.FrameLayout; import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.WindowManager; /** * Created by wangxingsheng on 2018/6/8. * * 可以设置最大宽高的FrameLayout * 默认优先比例设置 * 不支持参数小于零 * */ public class MaxLayout extends FrameLayout { private float mMaxHeightRatio = -1f;// 优先级高 private float mMaxHeight = -1f;// 优先级低 private float mMaxWidthRatio = -1f;// 优先级高 private float mMaxWidth = -1f;// 优先级低 private int parentWidth; private int parentHeight; private boolean firstHeightRatio = true;//高默认优先比例设置 private boolean firstWidthRatio = true;//宽默认优先比例设置 public MaxLayout(Context context) { super(context); } public MaxLayout(Context context, AttributeSet attrs) { super(context, attrs); initAttrs(context, attrs); } public MaxLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initAttrs(context, attrs); } private void initAttrs(Context context, AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MaxLayout); if(a!=null){ firstHeightRatio = a.getBoolean(R.styleable.MaxLayout_first_ratio_height,true); firstWidthRatio = a.getBoolean(R.styleable.MaxLayout_first_ratio_width,true); mMaxHeightRatio = a.getFloat(R.styleable.MaxLayout_max_height_ratio, -1f); mMaxHeight = a.getDimension(R.styleable.MaxLayout_max_height, -1f); mMaxWidthRatio = a.getFloat(R.styleable.MaxLayout_max_width_ratio, -1f); mMaxWidth = a.getDimension(R.styleable.MaxLayout_max_width, -1f); a.recycle(); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { initParentWH(); initWH(); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int maxHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize <= mMaxHeight ? heightSize : (int) mMaxHeight, heightMode); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int maxWidthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize <= mMaxWidth ? widthSize : (int) mMaxWidth, widthMode); super.onMeasure(maxWidthMeasureSpec, maxHeightMeasureSpec); } /** * 计算需要设置的宽高 */ private void initWH() { if((firstHeightRatio && mMaxHeightRatio > 0) || (!firstHeightRatio && mMaxHeight < 0)){ mMaxHeight = mMaxHeightRatio * parentHeight; } if((firstWidthRatio && mMaxWidthRatio > 0) || (!firstWidthRatio && mMaxWidth < 0)){ mMaxWidth = mMaxWidthRatio * parentWidth; } } /** * 获取父控件的宽高 */ private void initParentWH() { ViewGroup parent = (ViewGroup) getParent(); if(null != parent){ parentWidth = parent.getWidth(); parentHeight = parent.getHeight(); }else { WindowManager wm = (WindowManager) getContext() .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics dm = new DisplayMetrics(); if (wm != null) { wm.getDefaultDisplay().getMetrics(dm); parentWidth = dm.widthPixels; parentHeight = dm.heightPixels; } } } }
attrs.xml
<declare-styleable name="MaxLayout"> <attr name="max_height_ratio" format="float"/> <attr name="max_height" format="dimension"/> <attr name="max_width_ratio" format="float"/> <attr name="max_width" format="dimension"/> <attr name="first_ratio_height" format="boolean"/> <attr name="first_ratio_width" format="boolean"/> </declare-styleable>
相关文章推荐
- Android布局自定义Shap圆形ImageView,可以单独设置背景与图片
- Android 布局自定义Shap圆形ImageView,可以单独设置背景与图片
- Android中自定义textview可以进行自体设置
- Android布局自定义Shap圆形ImageView,可以单独设置背景与图片
- Android布局自定义Shap圆形ImageView可以单独设置背景与图片
- Android布局自定义Shap圆形ImageView,可以单独设置背景与图片
- android WebView设置最大高度
- 【Android 应用开发】 自定义组件 宽高适配方法, 手势监听器操作组件, 回调接口维护策略, 绘制方法分析 -- 基于 WheelView 组件分析自定义组件
- 自定义View:如果想对一个Android中已经存在的控件进行自定义的 话,可以有两个种方法
- Android中自定义ImageView添加文字设置按下效果详解
- Android笔记09/07 自定义可以自由滑动的view
- Android自定义View 一个可设置四个顶点弧度的ImageView
- Android自定View——可以设置宽高比例的ImageView
- android在自定义View的xml中设置自定义的成员属性
- (4.1.20.2)Android 5.0 可以给一个 View 单独设置一个 theme
- android 自定义View设置自定义监听 框架(监听自定义字符)
- Android 自定义View可以监听ACTION_DOWN却监听不到ACTION_MOVE和ACTION_UP
- 自定义可以设置drawable大小的TextView
- Android 自定义可以滚动的ViewGroup
- android中空的view,可以设置为水平线