zoukankan      html  css  js  c++  java
  • linearlayout和RelativeLayot的区别

    初学Android的朋友总是会困惑于布局方面的问题,甚至会怀念旧式VB开发中的绝对定位的方式(那绝对是个悲剧)。其实Android的Layout功能还是很强劲的,几乎可以让你不废吹灰之力的完成所有的布局要求,本篇教程会对Android中各个Layout的使用进行一个汇总。
    这是教程的第一部分,将会介绍最常用的2个Layout: LinearLayoutRelativeLayout
    相关教程:教程:Android各种Layout特性和使用汇总(二)介绍了TableLayout和FrameLayout。

    LinearLayout 线性布局

    特性:

    横向或纵向,每行或每列(根据方向)只能有1个对象,可以设置对象在LinearLayout中占位的权重。
    看上去很简单,也确实很简单,但LinearLayout却是Android中使用最多的布局之一,尤其是在多个Linear嵌套使用的时候,就会显得异乎寻常的强大。

    例子:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    
    <LinearLayout
    	xmlns:android="http://schemas.android.com/apk/res/android"
    	android:layout_width="fill_parent" android:layout_height="fill_parent"
    	android:orientation="vertical"
    	>
    	<TextView
    		android:text="Title" android:textSize="18dp" android:background="#AA3333FF"
    		android:layout_width="fill_parent" android:layout_height="wrap_content"
    	/>
    	<ListView
    		android:id="@+id/listViewInLinear"
    		android:layout_width="fill_parent" android:layout_height="wrap_content"
    		android:paddingTop="5dp" android:paddingBottom="5dp"
    		android:layout_weight="1"
    	/>
    	<LinearLayout
    		android:layout_width="fill_parent" android:layout_height="wrap_content"
    		android:orientation="horizontal"
    	>
    		<Button android:id="@+id/show"
    			android:layout_width="wrap_content" android:layout_height="wrap_content"
    			android:text="Show" android:layout_weight="50"
    		/>
    		<Button android:id="@+id/back"
    			android:layout_width="wrap_content" android:layout_height="wrap_content"
    			android:text="Back" android:layout_weight="50"
    		/>
    	</LinearLayout>
    </LinearLayout>

    下面是onCreate中ListView数据填充的方法,直接使用了APIDemo中的数据,用最简单的ArrayAdapter来实现。

    1
    2
    3
    4
    5
    6
    7
    
    	ListView arrowList=(ListView) findViewById(R.id.listViewInLinear);
    	arrowList.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, mStrings));
    	//mString定义
        private String[] mStrings = {
                "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", ...
    	}

    下面是运行截图,可以看到简单的2个LinearLayout嵌套外加layout_weight的使用,构造各分辨率自适应的布局是如此简单。
    LinearLayout Sample截图
    注意:同1个LinearLayout中可以设置多个子部件都有layout_weight属性,此时它们的大小会按weight的比例来进行分配,一般建议如果有多个weight,可以考虑让它们的和等于100。
    另外,如果设置了layout_weight,那么与LinearLayout相同方向的大小设置不要设置fill_parent,而是设置wrap_content比较好,否则会发生问题。(具体情形遇见便知)

    关键字:

    orientation(方向),layout_weight(权重),嵌套

    RelativeLayout 相对位置布局

    特性:

    布局内对象的位置可以相对于Layout本身来定位,也可以根据之前的对象来定位。非常适合用来表现多个对象对齐(或者故意不对齐?)的布局。
    上面这段描述像不像什么都没说?其实RelativeLayout可以做的事情基本上都可以由多个LinearLayout来实现,但在条件适合的时候用RelativeLayout来实现会更方便,更强大。而且有些情况下几乎只有RelativeLayout才能实现了。
    考虑以下这个布局的实现方式,
    预想的布局
    布局的第1行其实完全可以用LinearLayout来实现,只需要添加1个无文本的TextView在伸展区,并且设置layout_weight即可,虽然有些麻烦,但仍然可以实现。但第2行如何实现呢?要求是新的按钮与Right1右对齐。而用RelativeLayout就可以轻松的实现,下面是Layout定义文件:

    例子:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
    	xmlns:android="http://schemas.android.com/apk/res/android"
    	android:layout_width="fill_parent" android:layout_height="fill_parent"
    >
    	<Button android:id="@+id/relaLeftButton" android:text="Left"
    		android:layout_width="wrap_content" android:layout_height="wrap_content"
    		android:layout_alignParentLeft="true" android:layout_alignParentTop="true"
    	/>
    	<Button android:id="@+id/relaRight2Button" android:text="Right2"
    		android:layout_width="wrap_content" android:layout_height="wrap_content"
    		android:layout_alignParentRight="true" android:layout_alignParentTop="true"
    	/>
    	<Button android:id="@+id/relaRight1Button" android:text="Right1"
    		android:layout_width="wrap_content" android:layout_height="wrap_content"
    		android:layout_alignTop="@id/relaRight2Button" android:layout_toLeftOf="@id/relaRight2Button"
    	/>
    	<Button android:id="@+id/relaRight3Button" android:text="Right3Long"
    		android:layout_width="wrap_content" android:layout_height="wrap_content"
    		android:layout_alignRight="@id/relaRight1Button" android:layout_below="@id/relaRight1Button"
    	/>
    </RelativeLayout>

    在你使用了RelativeLayout之后,你会发现其中的控件都多了很多的layout_align开头的属性,有用于与父窗口对齐的,有用于和其它控件对齐和声明摆放位置的。如此便可以轻松的实现相对布局。下面是运行时截图:
    Relative布局运行时截图
    是不是达成要求了?虽然这是1个比较极端的例子,但在实际应用中,RelativeLayout的使用还是相当普遍的,通常用于登录画面之类的,元素不多,但要求布局相对宽松尽量利用空间的情况下。

  • 相关阅读:
    nodeJS入门01-http模块
    nodeJS入门-Buffer对象
    php与MySQL(php内置mysql函数)
    php与MySQL(基本操作)
    log4net
    js验证小数类型(浮点数)和整数类型
    牛腩学ASP.NET CORE做博客视频
    opencv再学习之路(八)---设定感兴趣区域(RIO)
    opencv再学习之路(四)---色彩分割得到二值图像
    opencv再学习之路(三)---形态学操作
  • 原文地址:https://www.cnblogs.com/songtzu/p/2586462.html
Copyright © 2011-2022 走看看