Android 自定义最大宽度,高度, 宽高比例 Layout,中高级Android面试中你不得不会的知识点

<com.xj.maxlayout.MaxLayout

android:layout_width=“match_parent”

android:layout_height=“100dp”

android:layout_marginTop=“15dp”

android:background=“@android:color/holo_blue_light”

app:ml_maxWidth=“200dp”>

<TextView

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_gravity=“center”

android:padding=“10dp”

android:text=“指定最大宽度,指定最大宽度,指定最大宽度” />

</com.xj.maxlayout.MaxLayout>

<com.xj.maxlayout.MaxLayout

android:layout_width=“200dp”

android:layout_height=“match_parent”

android:layout_marginTop=“15dp”

android:background=“@android:color/holo_blue_light”

app:ml_maxheight=“200dp”>

<TextView

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:layout_gravity=“center”

android:gravity=“center”

android:padding=“10dp”

android:text=“指定最大高度” />

</com.xj.maxlayout.MaxLayout>

<com.xj.maxlayout.MaxLayout

android:layout_width=“wrap_content”

android:layout_height=“match_parent”

android:layout_marginTop=“15dp”

android:background=“@android:color/holo_blue_light”

app:ml_maxWidth=“200dp”

app:ml_maxheight=“150dp”>

<TextView

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_gravity=“center”

android:padding=“10dp”

android:text=“同时指定最大宽度和最大高度” />

</com.xj.maxlayout.MaxLayout>

指定宽高比

指定宽高比,我们需要设置两个属性,ml_ratio_standard 和 ml_ratio。ml_ratio_standard 有两个值,w_h 代表已宽度为基准,h_w 代表已高度为基准。

比如,我们要指定高度是宽度的某个比例的时候,如,高度是宽度的两倍,可以这样写

<com.xj.maxlayout.MaxLayout

android:id=“@+id/ml_1”

android:layout_width=“100dp”

android:layout_height=“wrap_content”

android:background=“@color/colorAccent”

app:ml_ratio=“2.0”

app:ml_ratio_standard=“w_h”>

<ImageView

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:scaleType=“centerCrop”

android:src=“@mipmap/jsy03” />

</com.xj.maxlayout.MaxLayout>

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

比如,我们要指定宽度是高度的某个比例的时候,如,宽度是高度的 0.8,可以这样写

<com.xj.maxlayout.MaxLayout

android:id=“@+id/ml_2”

android:layout_width=“match_parent”

android:layout_height=“200dp”

android:layout_marginLeft=“20dp”

android:layout_toRightOf=“@id/ml_1”

android:background=“@android:color/holo_blue_light”

app:ml_ratio=“0.8”

app:ml_ratio_standard=“h_w”>

<ImageView

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:scaleType=“centerCrop”

android:src=“@mipmap/jsy04” />

</com.xj.maxlayout.MaxLayout>

[外链图片转存中…(img-6KDKARyi-1712363901860)]

当然,也可以同时指定比例和最大宽度,高度。

<com.xj.maxlayout.MaxLayout

android:id=“@+id/ml_03”

android:layout_width=“match_parent”

android:layout_height=“220dp”

android:layout_below=“@id/ml_1”

android:layout_marginTop=“15dp”

android:background=“@android:color/holo_blue_light”

app:ml_maxWidth=“150dp”>

<ImageView

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:scaleType=“centerCrop”

android:src=“@mipmap/jsy05” />

</com.xj.maxlayout.MaxLayout>

[外链图片转存中…(img-ZcXeFYy5-1712363901861)]


原理介绍


原理其实很简单,对自定义 View 有基本了解的人都知道,View 的宽度和高度,是在 onMeasure 方法中进行测量的,他们的大小受 MeasureSpec 的影响。既然如此,那么我们在继承 FrameLayout,重写它的 onMeasure 方法。在 onMeasure 方法中根据我们指定的最大宽度,高度和比例对 MeasureSpec 进行调整即可。

思路大概如下

  • 没有设置最大宽度,高度,宽高比例,不需要调整,直接返回

  • 先拿到原来的 mode 和 size,暂存起来

  • 根据宽高的比例进行相应的调整

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

// 是否设置了比例

boolean setRatio = isSetRatio();

// 没有设置最大宽度,高度,宽高比例,不需要调整,直接返回

if (mMaxWidth <= DEF_VALUE && mMaxHeight <= DEF_VALUE && !setRatio) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

return;

}

// 拿到原来宽度,高度的 mode 和 size

int heightMode = MeasureSpec.getMode(heightMeasureSpec);

int heightSize = MeasureSpec.getSize(heightMeasureSpec);

int widthMode = MeasureSpec.getMode(widthMeasureSpec);

int widthSize = MeasureSpec.getSize(widthMeasureSpec);

Log.d(TAG, “origin onMeasure: widthSize =” + widthSize + "heightSize = " + heightSize);

if (mRatioStandrad == W_H) { // 当模式已宽度为基准

widthSize = getWidth(widthSize);

if (mRatio >= 0) {

heightSize = (int) (widthSize * mRatio);

}

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

文末

那么对于想坚持程序员这行的真的就一点希望都没有吗?
其实不然,在互联网的大浪淘沙之下,留下的永远是最优秀的,我们考虑的不是哪个行业差哪个行业难,就逃避掉这些,无论哪个行业,都会有他的问题,但是无论哪个行业都会有站在最顶端的那群人。我们要做的就是努力提升自己,让自己站在最顶端,学历不够那就去读,知识不够那就去学。人之所以为人,不就是有解决问题的能力吗?挡住自己的由于只有自己。
Android希望=技能+面试

  • 技能
  • 面试技巧+面试题

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

行业难,就逃避掉这些,无论哪个行业,都会有他的问题,但是无论哪个行业都会有站在最顶端的那群人。我们要做的就是努力提升自己,让自己站在最顶端,学历不够那就去读,知识不够那就去学。人之所以为人,不就是有解决问题的能力吗?挡住自己的由于只有自己。
Android希望=技能+面试

  • 技能
    [外链图片转存中…(img-jLRnrldh-1712363901862)]
  • 面试技巧+面试题
    [外链图片转存中…(img-KelloF3q-1712363901862)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 19
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以自定义一个继承自FrameLayout的类,在类添加设置最大宽度最大高度的方法,同时在类添加相关的XML属性。代码示例如下: ```java public class CustomFrameLayout extends FrameLayout { private int mMaxWidth; private int mMaxHeight; public CustomFrameLayout(Context context) { super(context); } public CustomFrameLayout(Context context, AttributeSet attrs) { super(context, attrs); initAttrs(context, attrs); } public CustomFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initAttrs(context, attrs); } private void initAttrs(Context context, AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFrameLayout); mMaxWidth = a.getDimensionPixelSize(R.styleable.CustomFrameLayout_maxWidth, 0); mMaxHeight = a.getDimensionPixelSize(R.styleable.CustomFrameLayout_maxHeight, 0); a.recycle(); } /** * 设置最大宽度 * * @param maxWidth 最大宽度 */ public void setMaxWidth(int maxWidth) { mMaxWidth = maxWidth; requestLayout(); } /** * 设置最大高度 * * @param maxHeight 最大高度 */ public void setMaxHeight(int maxHeight) { mMaxHeight = maxHeight; requestLayout(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); // 如果设置了最大宽度,则取最大宽度与父容器宽度的最小值 if (mMaxWidth > 0 && widthMode != MeasureSpec.UNSPECIFIED) { widthSize = Math.min(widthSize, mMaxWidth); } // 如果设置了最大高度,则取最大高度与父容器高度的最小值 if (mMaxHeight > 0 && heightMode != MeasureSpec.UNSPECIFIED) { heightSize = Math.min(heightSize, mMaxHeight); } setMeasuredDimension(widthSize, heightSize); } } ``` 在res/values/attrs.xml文件添加自定义属性: ```xml <declare-styleable name="CustomFrameLayout"> <attr name="maxWidth" format="dimension" /> <attr name="maxHeight" format="dimension" /> </declare-styleable> ``` 然后在布局文件使用自定义FrameLayout并设置相关属性即可: ```xml <com.example.CustomFrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:maxWidth="500dp" app:maxHeight="500dp"> <!-- 添加子View --> </com.example.CustomFrameLayout> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值