Android布局--LinearLayout

本文详细介绍了Android开发中LinearLayout的应用,包括其属性设置如方向、对齐方式等,并通过实例展示了如何利用权重属性调整子控件的布局。

LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列。

1.LinearLayout的常用属性

android:orientation="vertical"

该属性决定其他子类控件的排布方式(vertical垂直;horizontal水平)

android:gravity="center"

该属性决定其他子类的xy的位置

常用属性值:

  1. >center_vertical  -- 垂直居中
  2. >center_horizontal -- 水平居中
  3. >center  -- 水平垂直都居中
  4. >right -- 右边
  5. >left -- 左边
  6. >bottom -- 下面

2.子类控件在LinearLayout中常用到的属性

android:layout_gravity="bottom"  ----指本身在当前父容器的XY的一个位置

android:layout_weight="1"        ----指本身控件占当前父容器的一个比例

实例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.demo4.MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="Button1"/>

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button2"/>

</LinearLayout>

效果如下:

转载于:https://my.oschina.net/daowuming/blog/749652

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值