在Android开发中,页面布局是非常重要的一部分。随着不同尺寸与分辨率的设备层出不穷,如何适配各种设备成为了一项必须掌握的技能。而要实现适配的过程,了解布局参数是必不可少的。今天我们就来探讨一下Android开发中常用的页面布局参数——layoutparams。
一、layoutparams概述
layoutparams即为布局参数,是Android中控制控件在布局中显示的参数。它是布局文件中控件标签的属性。每个View都有一个LayoutParams对象,不同的布局对应的LayoutParams也不同。如LinearLayout、RelativeLayout、FrameLayout等等都对应着不同的LayoutParams。
二、LinearLayout.LayoutParams
LinearLayout是Android中最基本的布局之一,它将所有子元素按照设定的方向(水平或垂直)在一条线上进行布局。下面来介绍一下LinearLayout.LayoutParams的相关属性。
1、android:layout_weight:控制控件在布局中分配空间的比例,此值越大,分配的空间越多。
举个例子,如下图所示的LinearLayout中有三个TextView,分别为左、中、右,想要让中间的TextView占整个屏幕宽度,只需要将中间的TextView的android:layout_weight设置为1即可。
```
android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1" android:background="#FF0000" android:text="Left" /> android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1" android:background="#00FF00" android:text="Center" /> android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1" android:background="#0000FF" android:text="Right" />
```
紫红绿字体的TextView均设置了android:layout_weight为1,其中中间的TextView就会自动占满整个屏幕,效果如下图所示:
2、android:layout_height和android:layout_width:这两个属性顾名思义,分别控制控件的高度和宽度,常用的属性值有match_parent、wrap_content和具体的数值。
比如,要让TextView的宽度自适应内容,可设置android:layout_width为wrap_content。如下代码:
```
android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> ``` 3、android:gravity:控制子控件的对齐方式,比如垂直居中、水平居中等等。 ``` android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> android:layout_width="wrap_content" android:layout_height="50dp" android:layout_gravity="center_vertical" android:background="#FF0000" android:text="Left" /> android:layout_width="wrap_content" android:layout_height="50dp" android:background="#00FF00" android:gravity="center" android:text="Center" /> android:layout_width="wrap_content" android:layout_height="50dp" android:background="#0000FF" android:text="Right" /> ``` 如上图所示,在第二个TextView中设置了android:gravity="center",便可以让文本在TextView中水平居中显示。 三、RelativeLayout.LayoutParams RelativeLayout是Android中又一种常用的布局方式,它可以实现更加复杂的布局效果。下面来介绍几个RelativeLayout.LayoutParams: 1、android:layout_alignParentLeft和android:layout_alignParentTop:控制控件相对于父布局的位置,android:layout_alignParentLeft即为控制控件相对于父布局的左侧距离,android:layout_alignParentTop即为控件的上侧距离。 ``` android:layout_width="match_parent" android:layout_height="match_parent"> android:id="@+id/left_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Left" android:layout_alignParentLeft="true" android:layout_alignParentTop="true"/> android:id="@+id/right_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Right" android:layout_alignParentRight="true" android:layout_alignParentTop="true"/> android:id="@+id/bottom_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bottom" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true"/> android:id="@+id/top_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Top" android:layout_centerHorizontal="true" android:layout_alignParentTop="true"/> ``` 如上图所示,通过android:layout_alignParentLeft、android:layout_alignParentRight、android:layout_centerHorizontal等属性可以让TextView相对于父布局进行对齐。 2、android:layout_alignLeft和android:layout_alignRight:控制控件相对于指定控件的对齐方式。熟悉CSS的同学肯定可以理解。 ``` android:layout_width="match_parent" android:layout_height="wrap_content"> android:id="@+id/left_tv" android:layout_width="wrap_content" android:layout_height="50dp" android:text="Left" /> android:id="@+id/right_tv" android:layout_width="wrap_content" android:layout_height="50dp" android:text="Right" android:layout_toRightOf="@+id/left_tv" android:layout_alignTop="@+id/left_tv" android:layout_alignBottom="@+id/left_tv" android:layout_alignRight="@+id/left_tv" /> ``` 如上图所示,让右侧的TextView相对于左侧的TextView进行对齐。 四、FrameLayout.LayoutParams FrameLayout是个简单的布局,它只能放置一个控件,下面介绍一下FrameLayout.LayoutParams: 1、android:layout_gravity:控制控件的位置,不同的控件有不同的表现。 ``` android:layout_width="match_parent" android:layout_height="match_parent"> android:layout_width="200dp" android:layout_height="200dp" android:src="@drawable/sample_img" android:layout_gravity="center" /> android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:textColor="#FFF" android:textSize="30dp" android:layout_gravity="center" /> ``` 如上图所示,通过android:layout_gravity属性,ImageView和TextView都在FrameLayout的中心显示。 2、android:layout_width和android:layout_height:控制控件的宽度和高度。 ``` android:layout_width="match_parent" android:layout_height="match_parent"> android:layout_width="200dp" android:layout_height="200dp" android:src="@drawable/sample_img" android:layout_gravity="center" /> android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:textColor="#FFF" android:textSize="30dp" android:layout_gravity="center" android:background="#000000" android:alpha="0.5" /> ``` 如上图所示,通过给TextView设置透明度,可以让其成为一个透明的半透明遮罩。 五、总结 到这里,针对Android中常用的布局之一LinearLayout、RelativeLayout和FrameLayout,小编已经为大家介绍了一些常用的LayoutParams属性,相信各位读者对如何使用layoutparams已经有了一定的了解。当然,掌握这些属性只是掌握布局的一部分,对于适配不同分辨率、处理好子控件与父布局的关系以及掌握常用控件属性等等都需要我们深入研究。望各位Android开发者在以后的开发中多多学习,不断成长。