Android两大布局:线性布局(LinearLayout) 相对布局(RelativeLayout)

来源:互联网 发布:淘宝转换微信链接 编辑:程序博客网 时间:2024/05/16 11:56

主要单词

RelativeLayout  LinearLayout orientation  vertical  horizontal  gravity


Android两大布局?惊讶

1.LinearLayout:

这种布局比较常用,就是每个元素占一行,当然也可能声明为横向排放,也就是每个元素占一列。

LinearLayout按照垂直或者水平的顺序依次排列子元素,每一个子元素都位于前一个元素之后。如果是垂直排列,那么将是一个N行单列的结构,每一行只会有一个元素,而不论这个元素的宽度为多少;如果是水平排列,那么将是一个单行N列的结构。如果搭建两行两列的结构,通常的方式是先垂直排列两个元素,每一个元素里再包含一个LinearLayout进行水平排列。


用处?

一般来说,当有很多控件需要在一个界面中列出来时,我们可以使用LinearLayout,

  线性布局常用属性:

  orientation: vertical(竖直);  horizontal;(水平)  

  gravity:控制子控件

  子控件的属性:
      1:weight:权重 权利越大 占的空间越大
          注意  layout_weight : 设置前要脱光了, 如果布局以垂直方向排版 那子控件的Layout_height(高度)就要设置为0dp,其次就是以水平方向排版  那子控件的Layout_width(宽度)就要为0dp
          2:layout_gravity:  控制控件放置位置

  注意  父容器  竖直方向 layout_gravity只能控制水平位置
                              水平方向 layout_gravity只能控制竖直位置        ( 备注:子元素尊重父元素,尊父原则)

  3:Android的高度有继承性会根据父元素的高绝定当前子元素的最大高度!


示例一:

实现效果:



效果代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">        <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        >        <Button            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="4"            android:text="1"            android:textSize="20dp"            android:paddingTop="20dp"            android:paddingBottom="20dp"            />        <Button            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="5"            android:text="2"            android:textSize="20dp"            android:paddingTop="20dp"            android:paddingBottom="20dp"            />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        >        <Button            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="7"            android:text="3"            android:textSize="20dp"            android:paddingTop="20dp"            android:paddingBottom="20dp"            />        <Button            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="3"            android:text="4"            android:textSize="20dp"            android:paddingTop="20dp"            android:paddingBottom="20dp"            />    </LinearLayout></LinearLayout>

示例二:

实现效果:



实现代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical"        >        <Button            android:layout_width="match_parent"            android:layout_height="0dp"            android:text="one"            android:gravity="left"            android:textSize="20dp"            android:paddingTop="10dp"            android:paddingBottom="10dp"            android:layout_weight="1"            />        <Button            android:layout_width="match_parent"            android:layout_height="0dp"            android:text="two"            android:textSize="20dp"            android:paddingTop="10dp"            android:paddingBottom="10dp"            android:layout_weight="8"            />        <Button            android:layout_width="match_parent"            android:layout_height="0dp"            android:gravity="right"            android:text="three"            android:textSize="20dp"            android:paddingTop="10dp"            android:paddingBottom="10dp"            android:layout_weight="1"            />    </LinearLayout>    </LinearLayout>

fsdfsdfsdf实

2.RelativeLayout

RelativeLayout按照各子元素之间的位置关系完成布局。在此布局中的子元素里与位置相关的属性将生效。

例如android:layout_below,  android:layout_above, android:layout_centerVertical等。

注意在指定位置关系时,引用的ID必须在引用之前,先被定义,否则将出现异常。

用处?

RelativeLayout是Android五大布局结构中最灵活的一种布局结构,比较适合一些复杂界面的布局。

注意:不能再RelativeLayout 容器本身和它的子元素之间产生依赖,比如,不能讲RelativeLayout 的高度设置为werp_content 时,

再将子元素的高度设置为Align_parent_bottom.

主要位置属性设置:


  



阅读全文
0 0
原创粉丝点击