Android 自定义最大宽度,高度, 宽高比例 Layout(1),android简历项目经验


常用的自定义属性

| key | 含义 | 补充说明 |

| — | — | — |

| ml_maxWidth | 最大宽度 | |

| ml_maxheight | 最大高度 | |

| ml_ratio_standard | 指定比例的模式,即是宽高比还是高宽比 | w_h,宽高比, h_w 高宽比 |

| ml_ratio | 比例值 | 只有比例模式是 w_h 或者 h_w,该值才会生效 |

指定最大宽度,高度

指定最大宽度,最大高度,我们值需要使用 ml_maxWidth,ml_maxheight 属性即可,当然我们也可以同时指定最大宽度和最大高度。如下

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

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

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

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

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

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

点击这里免费领取吧!

里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频**
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
[外链图片转存中…(img-9dI6qUGL-1711299797975)]

[外链图片转存中…(img-DVpAbCM8-1711299797975)]

[外链图片转存中…(img-N8Ddj87q-1711299797975)]

点击这里免费领取吧!

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值