【Android Studio】LinearLayout(线性布局)的相关属性

  • orientation

    控制线性布局中组件的排列方式,有horizontal(水平)和vertical(垂直,默认)两种方式。

  • layout_width

    布局的宽度,可以设置为wrap_content(组件实际大小),fill_parent (填满父容器),match_parent (填满父容器)和指定大小。

  • layout_height

    布局的高度,参数同layout_width

  • id

    为该组件设置一个资源id,在java文件中可以通过findViewById(id)找到该组件。

  • background

    为该组件设置一个背景图片,或者直接用颜色覆盖。

  • gravity

    控制组件所包含的子元素的对齐方式。

  • layout_gravity

    控制该组件在父容器里的对齐方式。

    eg.
    效果图:
    在这里插入图片描述
    实现代码:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/LinearLayout1"
     android:orientation="vertical"
     //设置为垂直布局
     tools:context=".MainActivity">
    
     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="请输入要保存的电话号码" />
    
     <EditText
         android:layout_width="fill_parent"
         android:layout_height="wrap_content" />
    
     <LinearLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
         //第三个组件设置为水平布局
         android:gravity="right" >
         //设置本组件所包含的子元素的排列方式为右对齐
    
         <Button
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="保存" />
    
         <Button
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="清空" />
     </LinearLayout>
    
    </LinearLayout>
    

    gravity注意:
    当android:orientation="horizontal"时,只有垂直方向的设置才起作用,水平方向的设置不起作用,即top,bottom,center_vertical是生效的;
    当android:orientation="vertical"时,只有水平方向的设置起作用,垂直方向的设置不起作用,即left,right,center_horizontal是生效的。

  • weight(权重)

    用来等比例地划分区域。
    ①最简单用法
    效果图1:
    [外链图片转存失败(img-bKyLvnTc-1564800054877)(http://www.runoob.com/wp-content/uploads/2015/07/89088844.jpg)]
    实现代码:

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/LinearLayout1"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="horizontal">
          
          <LinearLayout
              android:layout_width="0dp"
              android:layout_height="fill_parent"
              android:background="#ADFF2F"
              android:layout_weight="1" />
              
          <LinearLayout
              android:layout_width="0dp"
              android:layout_height="fill_parent"
              android:background="#DA70D6"
              android:layout_weight="1" />
      </LinearLayout>
    

    效果图2:
    在这里插入图片描述
    实现代码:
    只需将效果图1代码中两个LinearLayout的weight参数改成1和2就可以了。

    最简单用法总结:要等比例划分谁,就把谁的参数设为0,weight按比例设置即可。

    ②不设0的用法
    不把要等比例划分的对象设为0的话,存在两种情况:1)等比例划分的对象参数为wrap_content,2)等比例划分的对象参数为fill_parent或match_parent。
    1)warp_content比较简单,直接就按比例写就可以。
    效果图3:
    在这里插入图片描述
    实现代码:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
      xmlns:tools="http://schemas.android.com/tools"    
      android:id="@+id/LinearLayout1"    
      android:layout_width="match_parent"    
      android:layout_height="match_parent"  
      android:orientation="horizontal" >    
      
      <TextView    
          android:layout_weight="1"    
          android:layout_width="wrap_content"    
          android:layout_height="fill_parent"    
          android:text="one"     
          android:background="#98FB98" />
          
       <TextView    
          android:layout_weight="2"    
          android:layout_width="wrap_content"    
          android:layout_height="fill_parent"    
          android:text="two"     
          android:background="#FFFF00" />
          
       <TextView    
          android:layout_weight="3"    
          android:layout_width="wrap_content"    
          android:layout_height="fill_parent"    
          android:text="three"     
          android:background="#FF00FF" /> 
    </LinearLayout>
    

    2)match_parent(fill_parent)则需要计算
    我们写下面这段简单的代码:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
      xmlns:tools="http://schemas.android.com/tools"    
      android:id="@+id/LinearLayout1"    
      android:layout_width="match_parent"    
      android:layout_height="match_parent" >    
      
      <TextView    
          android:layout_weight="1"    
          android:layout_width="fill_parent"    
          android:layout_height="fill_parent"    
          android:text="one"     
          android:background="#98FB98"    
       />    
       <TextView    
          android:layout_weight="2"    
          android:layout_width="fill_parent"    
          android:layout_height="fill_parent"    
          android:text="two"     
          android:background="#FFFF00"    
       />    
       <TextView    
          android:layout_weight="3"    
          android:layout_width="fill_parent"    
          android:layout_height="fill_parent"    
          android:text="three"     
          android:background="#FF00FF"    
       />    
      </LinearLayout>
    

    效果图4:
    在这里插入图片描述
    这就和我们想象的不对了,本该是one:two:three=1:2:3,怎么变成one:two=2:1了?那么three又去了哪里?
    目前笔者只知道一种算法:
    ①:每一个都是match_parent,但是屏幕只有一个啊!
    那么1-3=-2match_parent;
    ②:依次比例是1/6,2/6,3/6;
    ③:先到先得,
    先给one,计算1-2*(1/6)=2/3match_parent,
    接着two,计算1-2*(2/6)=1/3match_parent,
    最后到three,计算1-2*(3/6)=0match_parent;
    ④:这就是为什么会出现效果图4所展示的情况了

    ③Java代码中设置weight属性

    setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
    				LayoutParams.WRAP_CONTENT, 1));
    
  • divider(分割线)

    该属性用于为LinearLayout设置分割线图片。

    showDividers

    设置分割线所在的位置,有四个可选值:none,middle,begining,end

    dividerPadding

    设置分割线的padding。

    eg.
    很多界面开发中都会设置一些下划线,或者分割线,从而使得界面更加整洁美观,比如下面的酷狗音乐的注册页面:
    在这里插入图片描述
    对于这种线,通常的做法有两种
    ①直接在布局中添加一个view,这个view的作用仅仅是显示出一条线,代码也很简单:

    <View  
        android:layout_width="match_parent"  
        android:layout_height="1px"  
        android:background="#000000" />  
    

    这是水平方向的一根黑线,如果不方便观察,我们也可以改成其他颜色或者使用图片。
    ②使用divider属性,直接为LinearLayout设置分割线。(需要准备分割线的图片)
    效果图5:
    在这里插入图片描述
    实现代码:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
      xmlns:tools="http://schemas.android.com/tools"  
      android:id="@+id/LinearLayout1"  
      android:layout_width="match_parent"  
      android:layout_height="match_parent"  
      android:divider="@mipmap/分割线图片"  
      android:orientation="vertical"  
      //垂直布局
      android:showDividers="middle" 
      //在两个组件之间出现 
      android:dividerPadding="10dp"
      //与上下组件间距为10dp  
      tools:context="com.jay.example.linearlayoutdemo.MainActivity" >  
    
      <Button  
          android:layout_width="wrap_content"  
          android:layout_height="wrap_content"  
          android:text="按钮1" />  
    
      <Button  
          android:layout_width="wrap_content"  
          android:layout_height="wrap_content"  
          android:text="按钮2" />  
    
      <Button  
          android:layout_width="wrap_content"  
          android:layout_height="wrap_content"  
          android:text="按钮3" />  
    
    </LinearLayout>
    
  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值