Android 应用开发(第3章)线性布局LinearLayout

 1、线性布局(LinearLayout)

  线性布局是Android中较为常用的布局方式,它使用<LinearLayout>标签。LinearLayout 线性布局有两种,分别是水平线性布局和垂直线性布局,LinearLayout属性中android:orientation为设置线性布局当其="vertical"时,为 垂直线性布局,当其="horizontal"时,为水平线性布局,不管是水平还是垂直线性布局一行(列)只能放置一个控件。

2. 创建空白文件 LinearLayoutTest1 工程


3、修改activity_main.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity= "bottom|center_horizontal">
    <Button
        android:id="@+id/bn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/bn1"/>
    <Button
        android:id="@+id/bn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/bn2"/>
    <Button
        android:id="@+id/bn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/bn3"/>
    <Button
        android:id="@+id/bn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/bn4"/>
    <Button
        android:id="@+id/bn5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/bn5"/>
</LinearLayout>

属性参数:

android:gravity    (一般容器才支持该属性,控制它所包含的子元素的对齐方式)

android:gravity意思是这个控件自己的“重力”,在通俗点就是控件上面的东西的位置(图片,文本等)

举个例子:一个TextView里面的文本默认居左作对齐的,你想让这些文本居中的话,只要在这个TextView的属性里加上android:gravity="center"

所有的参数分别是center(居中),bottom(下),top(上),right(右),left(左)
其他的一些是不常用的上面这些足够用了,比如要弄一个左下的android:gravity=" left| bottom "就OK了

android:layout_gravity 属性设置该子元素在父容器中的对齐方式

4、values 下的 strings.xml 文件如下:

  <resources>

    <string name="app_name">LinearLayoutTest1</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="bn1">按钮一</string>
    <string name="bn2">按钮二</string>
    <string name="bn3">按钮三</string>
    <string name="bn4">按钮四</string>
    <string name="bn5">按钮五</string>
</resources>

5、运行结果:


  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值