要点:

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

android:gravity="top"(buttom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical、clip_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的控件的宽度或者高度,再用剩余的宽度或高度按相应的比例来分配每一个控件显示的宽度或高度。

图片来自网络



#源代码

'''    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".LinearLayoutActivity" >
         android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

            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" >
       

                    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" >
       

                    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" >
       

                    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" >
       
   

            android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

                    android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="bottom"
            android:text="第1行"
            android:textSize="15sp" >
       

                    android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="bottom"
            android:text="第2行"
            android:textSize="15sp" >
       

                    android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="bottom"
            android:text="第3行"
            android:textSize="15sp" >
       

                    android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="bottom"
            android:text="第4行"
            android:textSize="15sp" >
       
   

'''

'''            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" >
       

                    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" >
       

                    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" >
       

                    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" >
       
   

            android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

                    android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="bottom"
            android:text="第1行"
            android:textSize="15sp" >
       

                    android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="bottom"
            android:text="第2行"
            android:textSize="15sp" >
       

                    android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="bottom"
            android:text="第3行"
            android:textSize="15sp" >
       

                    android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="bottom"
            android:text="第4行"
            android:textSize="15sp" >
       
   

'''

更多相关文章

  1. 安卓课程二十一 SeekBar拖动控件的使用
  2. Android(安卓)attrs文件(自定义)属性详解
  3. android:exported 属性详解
  4. AndroidManifest文件中android属性
  5. 学习Android界面开发: Shape Drawable
  6. Android精选完整源码之控件指示器视频压缩日历源码!
  7. Android中RelativeLayout布局各个xml相对布局属性的介绍和使用
  8. android layout属性介绍
  9. [android]控件ImageView的常用属性

随机推荐

  1. android扇形菜单
  2. android 碎片
  3. android增删改查sqlite数据库
  4. Android(安卓)按键事件流程概要
  5. Android打开页面
  6. 怎么正确学好画五官?漫画人物鼻子画法教程
  7. vite+vue3项目最佳起始点(保姆级)
  8. PHP生成PDF
  9. Redis和MySQL如何保持数据一致性?
  10. DBMesh 项目 DBPack 的实践