Android 自定义最大宽度,高度, 宽高比例 Layout

文章介绍了MaxLayout库如何通过重写onMeasure方法来控制View的宽度、高度和比例,以及MeasureSpec在其中的作用。作者强调了适应市场变化和技术提升的重要性。
摘要由CSDN通过智能技术生成

<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>

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

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

<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>

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


原理介绍


原理其实很简单,对自定义 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是不是快要凉了?而在我看来这正是市场成熟的表现,所有的市场都是温水煮青蛙,永远会淘汰掉不愿意学习改变,安于现状的那批人,希望所有的人能在大浪淘沙中留下来,因为对于市场的逐渐成熟,平凡并不是我们唯一的答案!

资料.png
资料图.jpg

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

.jpg" />

总结

作为一名从事Android的开发者,很多人最近都在和我吐槽Android是不是快要凉了?而在我看来这正是市场成熟的表现,所有的市场都是温水煮青蛙,永远会淘汰掉不愿意学习改变,安于现状的那批人,希望所有的人能在大浪淘沙中留下来,因为对于市场的逐渐成熟,平凡并不是我们唯一的答案!

[外链图片转存中…(img-qeoVQEK8-1712688962391)]
[外链图片转存中…(img-ZzO6llam-1712688962391)]

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

  • 24
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,在自定义的FrameLayout类中添加以下代码: ```java private int mMaxWidth = -1; private int mMaxHeight = -1; public void setMaxWidth(int maxWidth) { mMaxWidth = maxWidth; requestLayout(); } public void setMaxHeight(int maxHeight) { mMaxHeight = maxHeight; requestLayout(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int measuredWidth = MeasureSpec.getSize(widthMeasureSpec); int measuredHeight = MeasureSpec.getSize(heightMeasureSpec); if (mMaxWidth > 0 && measuredWidth > mMaxWidth) { measuredWidth = mMaxWidth; widthMeasureSpec = MeasureSpec.makeMeasureSpec(measuredWidth, MeasureSpec.EXACTLY); } if (mMaxHeight > 0 && measuredHeight > mMaxHeight) { measuredHeight = mMaxHeight; heightMeasureSpec = MeasureSpec.makeMeasureSpec(measuredHeight, MeasureSpec.EXACTLY); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } ``` 这里定义了两个变量mMaxWidth和mMaxHeight,分别表示最大宽度最大高度。然后提供了set方法,用于设置最大宽度最大高度。在onMeasure()方法中,对测量的宽高进行了判断,如果超过了最大宽高,则将其设置最大宽高。 接下来,在attrs.xml文件中添加以下代码: ```xml <declare-styleable name="MaxSizeFrameLayout"> <attr name="maxWidth" format="dimension" /> <attr name="maxHeight" format="dimension" /> </declare-styleable> ``` 这里定义了两个自定义属性,分别表示最大宽度最大高度。 最后,在xml布局文件中使用自定义FrameLayout时,可以通过以下方式设置最大宽度最大高度: ```xml <com.example.MaxSizeFrameLayout android:layout_width="match_parent" android:layout_height="match_parent" app:maxWidth="200dp" app:maxHeight="200dp"> <!-- 内容布局 --> </com.example.MaxSizeFrameLayout> ``` 这里设置最大宽度为200dp,最大高度也为200dp。注意,需要将app命名空间添加到根布局中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值