zoukankan      html  css  js  c++  java
  • Android线性布局(Linear Layout)

    Android 中常用布局
      [1] 线性布局
      [2] 相对布局
      [3] 帧布局           -- FrameLayout
      [4] 表格布局        -- !!!一个 tabrow 就代表一行
      [5] 绝对布局          -- android 中所有控件第一字母都大写

     

    LinearLayout(线性布局)

      要点:

        android:orientation="vertical"垂直线性布局,"horizontal"水平线性布局

        android:gravity="top"(buttomleftrightcenter_verticalfill_verticalcenter_horizontalfill_horizontalcenterfillclip_verticalclip_horizontal)

          控制布局中控件的对齐方式。如果是没有子控件的控件设置此属性,表示其内容的对齐方式,

          比如说TextView里面文字的对齐方式;若是有子控件的控件设置此属性,则表示其子控件的对齐方式,

          gravity如果需要设置多个属性值,需要使用“|”进行组合

        android:gravity android:layout_gravity的区别
        
    android:gravity是指定本元素的子元素相对它的对齐方式。
        
    android:layout_gravity是指定本元素相对它的父元素的对齐方式。

         android:layout_weight="1"通过设置控件的layout_weight属性以控制各个控件在布局中的相对大小,

          线性布局会根据该控件layout_weight值与其所处布局中所有控件layout_weight值之和的比值为该控件分配占用的区域。

          在水平布局的LinearLayout中有两个Button,这两个Button的layout_weight属性值都为1,那么这两个按钮都会被拉伸到整个屏幕宽度的一半

          如果layout_weight指0,控件会按原大小显示,不会被拉伸;对于其余layout_weight属性值大于0的控件,

          系统将会减去layout_weight属性值为0的控件的宽度或者高度,再用剩余的宽度或高度按相应的比例来分配每一个控件显示的宽度或高度。

     

    android:layout_gravity

    本元素相对于父元素的重力方向

    android:gravity 

    本元素所有子元素的重力方向

    android:orientation 

    线性布局以列或行来显示内部子元素

    android:layout_weight 

    子元素对未占用空间水平或垂直分配权重值

     

    当 android:orientation="vertical" 时,

     只有水平方向的设置才起作用,垂直方向的设置不起作用。

    即:left,right,center_horizontal 是生效的。!!!!

    当 android:orientation="horizontal" 时,

     只有垂直方向的设置才起作用,水平方向的设置不起作用。

    即:top,bottom,center_vertical 是生效的。!!!!

    android:layout_gravity 和 android:gravity 的区别

    android:gravity

    对元素本身起作用-本身元素显示在什么位置

    android:layout_gravity  

    相对与它的父元素-元素显示在父元素的什么位置。

    Button控件

    Button控件

    android:layout_gravity 

    表示button在界面上的位置

    android:gravity

    表示button上的字在button上的位置。

    可选值[多选时用“|”分开]top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。

    top

     将对象放在其容器的顶部,不改变其大小.

    bottom 

    将对象放在其容器的底部,不改变其大小.

    left

    将对象放在其容器的左侧,不改变其大小.

    right

    将对象放在其容器的右侧,不改变其大小.

    center_vertical 

    将对象纵向居中,不改变其大小.

    垂直对齐方式:垂直方向上居中对齐。

    垂直对齐方式:垂直方向上居中对齐。

    fill_vertical 

    必要的时候增加对象的纵向大小,以完全充满其容器. 垂直方向填充

    center_horizontal 

    将对象横向居中,不改变其大小水平对齐方式:水平方向上居中对齐

    fill_horizontal

    必要的时候增加对象的横向大小,以完全充满其容器. 水平方向填充

     center 

    将对象横纵居中,不改变其大小.

    fill 

    必要的时候增加对象的横纵向大小,以完全充满其容器.

    clip_vertical 附加选项,

    用于按照容器的边来剪切对象的顶部和/或底部的内容. 剪切基于其纵向对齐设置:顶部对齐时,剪切底部;底部对齐时剪切顶部;除此之外剪切顶部和底部.垂直方向裁剪

    clip_horizontal 附加选项,

    用于按照容器的边来剪切对象的左侧和/或右侧的内容. 剪切基于其横向对齐设置:左侧对齐时,剪切右侧;右侧对齐时剪切左侧;除此之外剪切左侧和右侧.水平方向裁剪

      

    例子

    TextView要让文本垂直/水平居中显示,有两种情况需要考虑:

      1、layout_width/layout_height为wrap_content,此时要让TextView在父控件上居中显示,必须设置layout_gravity=”center”。

      2、layout_width/layout_height为fill_parent,此时由于TextView已占据父窗体所有空间,必须设置gravity=”center”。

     

     

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        
    
        <LinearLayout 
            android:layout_weight="1"
             android:layout_width="match_parent"
                android:layout_height="match_parent"
             android:background="#c0c0c0"
             android:orientation="vertical"
              android:gravity="center"
            >
            
            <TextView 
                
                android:text="aaa"            
                 android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                android:textSize="50sp"
                android:textColor="#FF0000"
                />
        </LinearLayout>
        
        <LinearLayout 
             android:layout_weight="1"
             android:layout_width="match_parent"
                android:layout_height="match_parent"
             android:orientation="vertical"
            
            >
            <TextView 
                android:layout_height="30dp"
                android:layout_width="match_parent"
                android:background="#00ff00"
                />
            <LinearLayout 
                 android:layout_width="match_parent"
                    android:layout_height="match_parent"
                  android:orientation="horizontal" 
                >
                <TextView 
                     android:layout_width="30dp"
                        android:layout_height="match_parent"
                        android:background="#0000ff"
                        
                    />
                <RelativeLayout
                       android:layout_height="match_parent"
                       android:layout_weight="1"
                     >
                    <TextView  
                          android:id="@+id/tv_1"
                           android:layout_width="match_parent"
                             android:layout_height="40dp"
                           android:background="#888888"
                        />
                    <TextView 
                          android:id="@+id/tv_2"
                        
                          android:layout_below="@id/tv_1"
                          android:layout_width="match_parent"
                             android:layout_height="40dp"
                           android:background="#111111"
                        />
                    <TextView
                         android:id="@+id/tv_3"
                          android:layout_below="@id/tv_2"
                          android:layout_width="match_parent"
                               android:layout_height="40dp"
                           android:background="#00ffff"
                        />
                    <TextView                       
                          android:id="@+id/tv_4"
                          android:layout_below="@id/tv_3"
                          android:layout_width="match_parent"
                              android:layout_height="40dp"
                           android:background="#ffff00"
                        />
                  
                </RelativeLayout>
                <TextView 
                     android:layout_width="30dp"
                        android:layout_height="match_parent"
                        
                        android:background="#0000ff"                    
                    />
                
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".LinearLayoutActivity" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal" >
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#aa0000"
                android:gravity="center_horizontal|center_vertical"
                android:text="第一列"
                android:textSize="15sp" >
            </Button>
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#00aa00"
                android:gravity="center_horizontal"
                android:text="第二列"
                android:textSize="15sp" >
            </Button>
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#0000aa"
                android:gravity="center|bottom"
                android:text="第三列"
                android:textSize="15sp" >
            </Button>
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#aaaa00"
                android:gravity="bottom"
                android:text="第四列"
                android:textSize="15sp" >
            </Button>
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="bottom"
                android:text="第1行"
                android:textSize="15sp" >
            </Button>
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="bottom"
                android:text="第2行"
                android:textSize="15sp" >
            </Button>
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="bottom"
                android:text="第3行"
                android:textSize="15sp" >
            </Button>
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="bottom"
                android:text="第4行"
                android:textSize="15sp" >
            </Button>
        </LinearLayout>
    
    </LinearLayout>

    Android开发者Linear Layouts

    http://developer.android.com/guide/topics/ui/layout/linear.html

    Android布局---线性布局(Linear Layout)---别人翻译

    http://www.2cto.com/kf/201301/183527.html

    线性布局(Linear Layout)---理解应用

    http://hi.baidu.com/justtmiss/item/a5b59909c688a6e4ff240dac

     Android UI学习 - Linear Layout,RelativeLayout

    http://kb.cnblogs.com/page/73497/

     

  • 相关阅读:
    docker系列五之数据卷(volumn)
    docker系列四之docker镜像与容器的常用命令
    docker系列三之docker的安装
    Docker系列之入门
    idea Maven 插件 docker-maven-plugin 打包docker镜像上传到远程仓库
    mysql5.7 主从配置
    docker 私有仓库搭建
    PowerDesigner 把Comment复制到name中和把name复制到Comment
    docker 常用指令
    jenkins+gitlab+maven+docker自动化部署spring boot
  • 原文地址:https://www.cnblogs.com/Seven-cjy/p/6089144.html
Copyright © 2011-2022 走看看