Android学习第一天之LinearLayout浅析

布局篇——LinearLayout

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

二  1)基本属性:android:orientation="vertical\horizontal"指定线性布局的排列方式是竖向vertical还是横向horizontal,这是LinearLayout最关键的属性,默认值是横向horizontal

                     android:layout_width="" 指定布局的宽度
                     android:layout_height=""指定布局的高度

     2)常用属性:android:layout_weight=""它是用来分配属于空间的一个属性,你可以设置他的权重。这个得细讲,后面的章节中 我会深析此属性

                 android:layout_gravity=""是用来设置该view相对与父view 的位置.比如一个高度为40dp宽度为120dp的linearlayout在父linearlayout里,你想把该linearlayout放在父linearlayout里靠右等位置就可以通过该属性设置. 


对应代码示例

<?xml version="1.0" encoding="utf-8"?>
<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="120dp"
         android:layout_height="40dp"
         android:background="#D2B48C"
         android:layout_gravity="right" >
    </LinearLayout>
</LinearLayout>

              android:gravity="" 属性是对该view中内容的限定,限定该View里面内容(可能是一个控件,布局,或者控件里面的字符串)的位置.

例如 对上图所示的布局 我们还可以这样写代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="right"
    android:orientation="vertical" >
    <LinearLayout 
         android:layout_width="120dp"
         android:layout_height="40dp"
         android:background="#D2B48C">
    </LinearLayout>
</LinearLayout>


最后写几个demo练习一下LinearLayout 布局


                         

对应xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#FFB6C1"
            android:layout_weight="1"
            android:text="@string/one_1"/>
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#4169E1"
            android:layout_weight="2"
            android:text="two"/>
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00FFFF"
            android:layout_weight="3"
            android:text="three"/>
        </LinearLayout>
        <TextView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:background="#DEB887"
            android:text="four"/>

</LinearLayout>

对应的XML文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left|center_vertical" 
        android:background="#D2B48C"
        android:layout_gravity=""
        android:layout_weight="1"
        android:text="one"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:background="#98FB98"
        android:gravity="center" 
        android:text="two"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#FF69B4"
        android:gravity="right|center_vertical" 
        android:text="three"/>
  
</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值