日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android自定义View——可以设置最大宽高的FrameLayout

發布時間:2023/12/15 Android 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android自定义View——可以设置最大宽高的FrameLayout 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為什么80%的碼農都做不了架構師?>>> ??

可以設置最大寬高的FrameLayout

支持相對父控件的半分比設置

默認優先比例設置

不支持參數小于零

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();}}@Overrideprotected 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>

?

轉載于:https://my.oschina.net/reone/blog/1826781

總結

以上是生活随笔為你收集整理的Android自定义View——可以设置最大宽高的FrameLayout的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。