美文网首页
Android布局(一)--- LinearLayout(线性布

Android布局(一)--- LinearLayout(线性布

作者: GODANDDEVIL | 来源:发表于2020-02-21 10:13 被阅读0次

当android:orientation="vertical"时,子View只有水平方向的设置生效,当android:orientation="horizontal"时,子View只有垂直方向的设置生效。父元素设置的gravity优先级要低于子元素设置的layout_gravity。

1、线性布局中的元素属性

android:gravity 表示该组件的子元素的对齐方式

android:layout_gravity 表示该组件在其父元素内的对其方式

android:layout_width 表示该组件宽度

android:layout_height 表示该组件高度

android:id 表示该组件资源id

android:background 表示该组件背景

android:weight 表示该组件的权重

android:layout_margin 外边距

android:layout_padding 内边距

2、weight(权重)属性详解:

LinearLayout有一个权重数量的标记:weightSum,在LinearLayout中没有声明weightSum时,默认的就是各个控件权重的总和

①最简单用法:

如图:

实现代码:

<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="2"/>   

        </LinearLayout> 

要实现第一个的1:1的效果,只需要分别把两个LinearLayout的weight改成1和1就可以了。 按比例划分水平方向:将涉及到的View的android:width属性设置为0dp,然后设置为android weight属性设置比例即可,如果是竖直方向,只需设android:height为0dp,然后设weight属性即可。

②weight属性详解:

当然,如果我们不适用上述那种设置为0dp的方式,直接用wrap_content和match_parent的话,则要接着解析weight属性了,分为两种情况:wrap_content与match_parent。另外还要看 LinearLayout的orientation是水平还是竖直,这个决定哪个方向等比例划分。

1)wrap_content比较简单,直接就按比例的了

实现代码:

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

运行效果图:

系统先给第1个子控件分配parent_width(剩余空间),再给第2个分配parent_width,再给第3个分配parent_width,即分配了3个parent_width,此时剩余空间为 parent_width - 3parent_width = -2parent_width。接着,第一个控件占宽度:parent_width(当前已有宽度)+ 权重1/6*(-2parent_width) = 2/3parent_width;第二个控件占宽度:parent_width + 权重1/3*(-2parent_width) = 1/3parent_width;第三个控件占宽度:parent_width + 权重1/2*(-2parent_width) = 0parent_width。得出公式:C表示子布局声明的大小,B表示剩余布局的大小,P表示子布局占据父布局剩余布局的比例,则子布局最终的实际大小R为:R = C + B * P。

0dp与wrap_content

谷歌官方建议使用0dp,来分比例显示布局,和wrap_content大同小异,当使用layout_weight时,都表示占据剩余宽度或高度的比重。

但两者有明显区别。

使用0dp时,要考虑所分配的布局宽度是否小于控件实际宽度,比如:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/linear_layout"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@android:color/holo_blue_dark"android:padding="5dp"android:text="left"/><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="19"android:background="@android:color/holo_green_dark"android:gravity="center"android:padding="5dp"android:text="right"/></LinearLayout>

则显示为如下:

而使用wrap_content则不会出现上面的那种情况

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/linear_layout"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:background="@android:color/holo_blue_dark"android:padding="5dp"android:text="left"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="19"android:background="@android:color/holo_green_dark"android:gravity="center"android:padding="5dp"android:text="right"/></LinearLayout>

这里left先获取自适应的大小,然后再去获取剩余布局的1/20。

0dp与match_parent

可以发现match_parent是与0dp的效果是相反的

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/linear_layout"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:background="@android:color/holo_blue_dark"android:padding="5dp"android:text="left"/><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="2"android:background="@android:color/holo_green_dark"android:gravity="center"android:padding="5dp"android:text="right"/></LinearLayout>

权重计算:
控件的实际大小 = 控件设定的大小 + (布局剩余的大小) * 布局的比例

布局剩余的大小 = 父布局的大小 - 子布局大小之和

3、LinearLayout 设置分割线

(1)直接在布局中添加一个view,作用仅仅是显示出一条线

<View  

    android:layout_width="match_parent"  

    android:layout_height="1px"  

    android:background="#000000" />

(2)使用LinearLayout的divider属性

第一步准备一张分割线的图片;第二步 android:divider 设置作为分割线的图片;第三步 android:showDividers 设置分割线的位置,none(无)、begining(开始)、end(结束)、middle(每两个组件之间);第四步 android:dividerPadding 设置分割线的Padding。

android:divider="@drawable/cut_off_rule"

android:showDividers="middle"

android:dividerPadding="5dp"

相关文章

  • Android常见布局

    在Android中免不了用到布局,今天主要学习下Android常见布局 线性布局(LinearLayout) 帧布...

  • 布局ViewGroup原理解析(二):LinearLayout

    本节引言 本节开始讲Android中的布局,Android中有六大布局,分别是: LinearLayout(线性布...

  • android基础

    布局基本布局 FrameLayout线性布局 LinearLayout相对布局 RelativeLayout绝对布...

  • 2、Android中布局和单位

    Android中所有的控件第一个字母都大写 Android的五大布局 线性布局(LinearLayout) 相对布...

  • 2020-10-06

    Android常见界面布局:RelativeLayout(相对布局) LinearLayout(线性...

  • Android布局(一)--- LinearLayout(线性布

    当android:orientation="vertical"时,子View只有水平方向的设置生效,当androi...

  • Android - 4种基本布局

    1.线性布局 LinearLayout:线性布局 EditText 和 Button 的 android:layo...

  • Android控件

    vertial 水平horizontal 垂直 布局 : LinearLayout 线性布局android:l...

  • 入门

    一、界面布局介绍 RelativeLayout(相对布局)和LinearLayout(线性布局)android:l...

  • 布局管理

    LinearLayout(线性布局) 基本属性: ①android:orientation android:ori...

网友评论

      本文标题:Android布局(一)--- LinearLayout(线性布

      本文链接:https://www.haomeiwen.com/subject/aeuxqhtx.html