线性布局(LinearLayout)
线性布局是Android中较为常用的布局方式,它使用<LinearLayout>标签。线性布局主要有两种形式,一种是水平线性布局,一种是垂直线性布局,如图所示:
1.在Androidstudio开发环境中建立UI界面切换至Android视图依次打开app-res-layout,在layout中选择新建XML文件(linearlayout_demo1.xml)编写代码,通过修改代码进行线性布局的水平或者垂直两种形式的转换,其代码如下:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button3" />
2.然后建立名为linearlayout_demo2.xml的XML文件进行UI界面中控件的位置变换,代码如下:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" android:text="Button1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Button2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:text="Button3" />
3.重要属性—android:layout_weight
android:layout_weight属性允许我们使用比例的方式来指定空间的大小,它在手机屏幕的适配性方面可以起到非常重要的作用。
建立名为linearlayout_demo3.xml的XML文件运用android:layout_weight属性编写如下代码:
<EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Type someting" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send" />