android 线性布局总结

本文详细解析了Android线性布局的特点及使用方法,包括水平和垂直排列方式、重力属性的作用,以及layout-gravity与gravity的区别,帮助开发者掌握布局技巧。

线性布局:

线性布局特点,同一级组件之间没有互相依赖

按照添加顺序依次排列,其中线性布局最重要的属性是android:orientation

通过该属性可以指定水平方向排列还是纵向排列

orientation="vertical"水平方向

orientation="horizontal" 垂直方向

注意: Android 的线性布局不会换行,当组件一个挨着一个的排列到头之后,

            剩下的组件将不会被显示出来

<?xml version="1.0" encoding="utf-8"?>
    <!--线性布局特点,同一级组件之间没有互相依赖,-->
    <!--按照添加顺序依次排列,其中线性布局最重要的属性是android:orientation-->
    <!--通过该属性可以指定水平方向排列还是纵向排列
    orientation="vertical"水平方向
    orientation="horizontal" 垂直方向
    -->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="96dp"
            android:layout_height="match_parent"
            android:background="#b2dfdb"
            android:text="第一块"/>
        <TextView
            android:layout_width="96dp"
            android:layout_height="match_parent"
            android:background="#80cbc4"
            android:text="第二块"/>
        <TextView
            android:layout_width="96dp"
            android:layout_height="match_parent"
            android:background="#4db6ac"
            android:text="第三块"/>
        <TextView
            android:layout_width="96dp"
            android:layout_height="match_parent"
            android:background="#26a69a"
            android:text="第四块"/>
       <TextView
            android:layout_width="96dp"
            android:layout_height="match_parent"
            android:background="#26a69a"
            android:text="第五块"/>
     <TextView
            android:layout_width="96dp"
            android:layout_height="match_parent"
            android:background="#CDDC39"
            android:text="第六块"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="68dp"
            android:background="#b2dfdb"
            android:text="第一块"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="68dp"
            android:background="#80cbc4"
            android:text="第二块"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="68dp"
            android:background="#4db6ac"
            android:text="第三块"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="68dp"
            android:background="#26a69a"
            android:text="第四块"/>
    </LinearLayout>

</LinearLayout>

                     

 

线性布局 gravity(重力)属性:

    设置布局管理器内组件的对齐方式,该属性值:

  top(顶部对齐) 、bottom 、left 、right 、center_vertical(垂直方向居中) 、 fill_vertical(垂直方向填充) 、 center_horizontal(水平方向居中) 、 fill_horizontal(水平方向填充) 、center(垂直与水平方向都居中) 、 fill (填充)、 clip_vertical(垂直方向裁剪) 、 clip_horizontal(水平方向裁剪) 。可同时指定多种对其方式的组合,中间用“|”连接,

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="bottom">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="96dp"
            android:layout_height="match_parent"
            android:background="#b2dfdb"
            android:text="第一块"/>
        <TextView
            android:layout_width="96dp"
            android:layout_height="match_parent"
            android:background="#80cbc4"
            android:text="第二块"/>
        <TextView
            android:layout_width="96dp"
            android:layout_height="match_parent"
            android:background="#4db6ac"
            android:text="第三块"/>
    </LinearLayout>

 

                                                              

 

布局中layout-gravity与gravity的区别

1:两者可设置的值是相同的

2: gravity是设置自身内部元素的对齐方式。

      layout_gravity是设置自身相当于父容器的对齐方式。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/父组件"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#F44336"
    android:gravity="bottom"
    >

    <LinearLayout
        android:id="@+id/子组件"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:orientation="horizontal"
        >
        <TextView
            android:id="@+id/ws1"
            android:layout_width="96dp"
            android:layout_height="150dp"
            android:background="#b2dfdb"
            android:gravity="bottom"
            android:layout_gravity="top"
            android:text="第一块"/>
        <TextView
            android:id="@+id/ws2"
            android:layout_width="96dp"
            android:layout_height="150dp"
            android:background="#80cbc4"
            android:text="第二块"/>
        <TextView
            android:id="@+id/ws3"
            android:layout_width="96dp"
            android:layout_height="150dp"
            android:background="#4db6ac"
            android:text="第三块"/>
         
  </LinearLayout>

                                                          

注解:

       顶级父组件设置了android:gravity="bottom"规定了其内部组件(元素)的对齐方式 在子组件中有设有三个TextView组件android:gravity="bottom"设置了其内部内容的对齐方式android:layout_gravity="top"设置了组件id为ws1的TextView组件相对于其直接父组件的排列方式。

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值