Android中使控件保持固定宽高比的几种方式

简介: 我们在android开发过程中可能会遇到一种情况,一个组件需要保持固定的宽高比,但是组件本身大小却不定。比如我们需要让一个组件宽度与屏幕宽度一致,这样就无法确定宽度。那么如何让控件保持固定宽高比?有几种方法供大家选择。

前言


我们在android开发过程中可能会遇到一种情况,一个组件需要保持固定的宽高比,但是组件本身大小却不定。比如我们需要让一个组件宽度与屏幕宽度一致,这样就无法确定宽度。那么如何让控件保持固定宽高比?有几种方法供大家选择。


自定义View


自定义view,重写onMeasure或onLayout等相关方法,通过预定的比例计算宽高。

下面是简单示意:


@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
     int width = MeasureSpec.getSize(widthMeasureSpec);
     if (mRatio != 0) {
         float height = width / mRatio;
         heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) height, MeasureSpec.EXACTLY);
     }
     super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
复制代码

这种方法是很多早期开发者喜欢的方式,但是缺点是需要自己重新自定义一个view。


adjustViewBounds


为ImageView设置adjustViewBounds,如下:

android:adjustViewBounds="true"
复制代码

这样ImageView就会以图片的宽高比显示。

但是这个方法的缺点是只能用于ImageView。


百分比布局


Android提供了Android-percent-support这个库,支持百分比布局,包括PercentRelativeLayout和PercentFrameLayout。

使用PercentFrameLayout也可以实现一个组件的固定比例显示,代码如下:


<android.support.percent.PercentFrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="fitXY"
        app:layout_widthPercent="100%"
        app:layout_aspectRatio="@fraction/circle_article_aspectRatio"
        />
</android.support.percent.PercentFrameLayout>
复制代码


需要在res/values下新建一个fraction.xml,代码如下:


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="circle_article_aspectRatio" type="fraction">133%</item>
</resources>
复制代码


这样就实现了宽高4:3的比例。

这个方法的优点是不必自定义view。缺点是组件外层需要包裹一个百分比布局,同时需要一个设置ratio的xml文件。


ConstraintLayout


这种方式与百分比布局类似,使用的是ConstraintLayout的DimensionRatio属性,代码如下:


<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:src="@mipmap/bb"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintDimensionRatio="4:3"
        />
</android.support.constraint.ConstraintLayout>
复制代码


这种方法的优点是不用自定义view,相对于百分比布局不需要创建一个设置ratio的xml文件;缺点是需要使用ConstraintLayout。

在上面示例中我们将ImageView的宽高都设置为0。就此我测试了其他的可能性,产生的几个情况如下:


1、如果组件宽高都设置0dp,组件宽高按比例,且只受父view的约束。如图

网络异常,图片无法展示
|


2、如果其中一个设置成了wrap_content,比如说宽度,那么宽度就会是 图片的真实宽度 和 父view的限制宽度 的较小值,而高度会根据宽度和比例计算出来。

这时如果图片较小,就不会撑满父View。如图

网络异常,图片无法展示
|


3、如果宽高都设置成了wrap_content或match_parent,则比例失效。如图:

网络异常,图片无法展示
|


注意:


1、当宽高都设置为0dp时

app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
复制代码

不能省略,否则组件大小为0。


2、当宽高其中一个或都设置为wrap_content时,如果去掉

app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
复制代码

则比例失效。


目录
相关文章
|
2天前
|
XML Android开发 数据格式
Android基础控件介绍
Android基础控件介绍
4 0
|
27天前
|
Java Android开发
18. 【Android教程】图片控件 ImageView
18. 【Android教程】图片控件 ImageView
17 4
|
27天前
|
前端开发 API Android开发
25. 【Android教程】列表控件 ListView
25. 【Android教程】列表控件 ListView
26 2
|
27天前
|
Java Android开发 开发者
17. 【Android教程】开关控件ToggleButton/Switch
17. 【Android教程】开关控件ToggleButton/Switch
19 2
|
9天前
|
Android开发
Android 自定义View 测量控件宽高、自定义viewgroup测量
Android 自定义View 测量控件宽高、自定义viewgroup测量
12 0
|
9天前
|
开发工具 Android开发 git
Android自定义View——可以设置最大宽高的FrameLayout
Android自定义View——可以设置最大宽高的FrameLayout
20 0
|
9天前
|
JSON Android开发 数据格式
Android动态添加view设置view大小(宽高)
Android动态添加view设置view大小(宽高)
10 0
|
1月前
|
XML Java Android开发
Android控件动态使用 (转)
Android控件动态使用 (转)
16 1
|
1月前
|
XML Java Android开发
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
Android控件之基础控件——进度条类的view——TextView、Checkbox复选控件、RadioButton单选控件、ToggleButton开关、SeekBar拖动条、menu、弹窗
103 1
|
1月前
|
XML 编解码 Java
Android控件之高级控件——ListView、cardView、屏幕适配
Android控件之高级控件——ListView、cardView、屏幕适配