Android 5大布局

来源:互联网 发布:ps4版辐射4捏脸数据 编辑:程序博客网 时间:2024/05/16 08:52

 

五类布局layouts

LinearLayout

LinearLayout(线性布局)提供了控件水平垂直排列的模型,同时可以通过设置子控件的weight布局参数控制各个控件在布局中的相对大小。水平(vertical)垂直(horizontal)

  fill-parent:占满整个屏幕,wrap-content:刚好适合控件内容的大小

  对齐方式gravity取值:

  top:不改变大小,位置置于容器的顶部

  bottom:不改变大小,位置置于容器的底部

  left:不改变大小,位置置于容器的左边

  right:不改变大小,位置置于容器的右边

  center_vertical:不改变大小,位置置于容器的纵向中央部分

  center_horizontal:不改变大小,位置置于容器的横向中央部分

  center:不改变大小,位置置于容器的横向和纵向的中央部分

  fill_vertical:可能的话,纵向延伸可以填满容器

  fill_horizontal:可能的话,横向延伸可以填满容器

  fill:可能的话,纵向和横向延伸填满容器



RelativeLayout

RelativeLayout(相对布局)允许子元素指定他们相对于其它元素或父元素的位置(通过ID指定)。因此,你可以以右对齐,或上下,或置于屏幕中央的形式来排列两个元素。元素按顺序排列,因此如果第一个元素在屏幕的中央,那么相对于这个元素的其它元素将以屏幕中央的相对位置来排列。如果使用XML来指定这个layout,在你定义它之前,被关联的元素必须定义


android:id --- 为控件指定相应的ID
android:text --- 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串
android:grivity --- 指定控件的基本位置,比如说居中,居右等位置这里指的是控件中的文本位置并不是控件本身。
android:textSize --- 指定控件当中字体的大小
android:background --- 指定该控件所使用的背景色,RGB命名法
android:width --- 指定控件的宽度
android:height --- 指定控件的高度
android:padding* --- 指定控件的内边距,也就是说控件当中的内容 android:padding="3dip"说明了四边的内边距是3dip
android:sigleLine --- 如果设置为真的话,则控件的内容在同一行中进行显示

padding表示填充,margin表示边距 

//相对于父控件

Android:layout_centerInparent, 将当前控件放置于起父控件的横向和纵向的中央部分
Android:layout_centerHorizontal, 使当前控件置于父控件横向的中央部分 
Android:layout_centerVertival,  使当前控件置于父控件纵向的中央部分
Android:layout_alignParentLeft,使当前控件的左端和父控件左端对齐 
  Android:layout_alignParentRight,使当前控件的右端和父控件右端对齐 
  Android:layout_alignParentTop,  使当前控件的顶端和父控件顶端对齐 
  Android:layout_alignParentBottom,使当前控件的底端和父控件底端对齐 
  上述属性只能设置Bool类型的值,“true”或“false” 

Android:layout_below/layout_above/ layout_toLeftOf/layout_toRightOf =“@id/”使当前控件置于给出id的空间的下方/上方/左边/右边 
Android:layout_marginBottom/layout_marginLeft/layout_marginRight/layout_marginTop=”30px”使当前控件底部/左边/右边/顶部空出相应像素空间

     // 相对于给定ID控件
android:layout_above 将该控件的底部置于给定ID的控件之上;
android:layout_below 将该控件的底部置于给定ID的控件之下;
android:layout_toLeftOf 将该控件的右边缘与给定ID的控件左边缘对齐;
android:layout_toRightOf 将该控件的左边缘与给定ID的控件右边缘对齐;
android:layout_alignBaseline 将该控件的baseline与给定ID的baseline对齐;
android:layout_alignTop 将该控件的顶部边缘与给定ID的顶部边缘对齐;
android:layout_alignBottom 将该控件的底部边缘与给定ID的底部边缘对齐;
android:layout_alignLeft 将该控件的左边缘与给定ID的左边缘对齐;
android:layout_alignRight 将该控件的右边缘与给定ID的右边缘对齐;
// 指定移动像素
android:layout_marginTop 上偏移的值;
android:layout_marginBottom 下偏移的值;
android:layout_marginLeft   左偏移的值;此属性用来设置控件之间的间隙(控件和控件之间和内边距不同)
android:layout_marginRight   右偏移的值;




TableLayout

1、TableLayout简介

2、TableLayout行列数的确定
3、TableLayout可设置的属性详解
4、一个包含4个TableLayout布局的实例及效果图


一、Tablelayout简介      

Tablelayout类以行和列的形式对控件进行管理,每一行为一个TableRow对象,或一个View控件。        当为TableRow对象时,可在TableRow下添加子控件,默认情况下,每个子控件占据一列。        当为View时,该View将独占一行。

二、TableLayout行列数的确定        

TableLayout的行数由开发人员直接指定,即有多少个TableRow对象(或View控件),就有多少行。
       TableLayout的列数等于含有最多子控件的TableRow的列数。如第一TableRow含2个子控件,第二个TableRow含3个,第三个TableRow含4个,那么该TableLayout的列数为4.
三、TableLayout可设置的属性详解

TableLayout可设置的属性包括全局属性及单元格属性。
1、全局属性也即列属性,有以下3个参数:

android:stretchColumns   设置可伸展的列。该列可以向行方向伸展,最多可占据一整行。

android:shrinkColumns    设置可收缩的列。当该列子控件的内容太多,已经挤满所在行,那么该子控件的内容将往列方向显示。

android:collapseColumns设置要隐藏的列。

示例:

android:stretchColumns="0"          第0列可伸展

android:shrinkColumns="1,2"        第1,2列皆可收缩

android:collapseColumns="*"        隐藏所有行
说明:列可以同时具备stretchColumns及shrinkColumns属性,若此,那么当该列的内容N多时,将“多行”显示其内容。(这里不是真正的多行,而是系统根据需要自动调节该行的layout_height)

2、单元格属性,有以下2个参数:

android:layout_column   指定该单元格在第几列显示
android:layout_span       指定该单元格占据的列数(未指定时,为1)

示例:
android:layout_column="1"   该控件显示在第1列
android:layout_span="2"       该控件占据2列
说明:一个控件也可以同时具备这两个特性。

四、一个包含4个TableLayout布局的实例及效果图


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="3dip"
    >
   
<!-- 1TableLayout,用于描述表中的列属性。第0列可伸展,第1列可收缩,第2列被隐藏-->
    <TextView
  android:text="表1:全局设置:列属性设置"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:textSize="15sp"
  android:background="#7f00ffff"/>
<TableLayout   
  android:id="@+id/table1"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:stretchColumns="0"
  android:shrinkColumns="1"
  android:collapseColumns="2"
  android:padding="3dip">
  <TableRow>
   <Button android:text="该列可伸展"/>
   <Button android:text="该列可收缩"/>
   <Button android:text="我被隐藏了"/>
  </TableRow>
  
  <TableRow>
   <TextView android:text="我向行方向伸展,我可以很长    "/>
   <TextView android:text="我向列方向收缩,我可以很深"/>
  </TableRow>  
  
</TableLayout>

<!-- 2TableLayout,用于描述表中单元格的属性,包括:android:layout_columnandroid:layout_span-->
<TextView
  android:text="表2:单元格设置:指定单元格属性设置"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:textSize="15sp"
  android:background="#7f00ffff"/>
<TableLayout
  android:id="@+id/table2"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:padding="3dip">
  <TableRow>
   <Button android:text="第0列"/>
   <Button android:text="第1列"/>
   <Button android:text="第2列"/>
  </TableRow>
  
  <TableRow>
   <TextView android:text="我被指定在第1列"android:layout_column="1"/>
  </TableRow>
   
  <TableRow>
   <TextView
    android:text="我跨1到2列,不信你看!"
    android:layout_column="1"
    android:layout_span="2"
    />
  </TableRow>
  
</TableLayout>

<!-- 3TableLayout,使用可伸展特性布局-->
<TextView
  android:text="表3:应用一,非均匀布局"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:textSize="15sp"
  android:background="#7f00ffff"/>
<TableLayout
  android:id="@+id/table3"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:stretchColumns="*"
  android:padding="3dip"
  >
  <TableRow>
   <Button android:text="一" ></Button>
   <Button android:text="两字"></Button>
   <Button android:text="三个字" ></Button>
  </TableRow>
</TableLayout>

<!-- 4TableLayout,使用可伸展特性,并指定每个控件宽度一致,如1dip-->
<TextView
  android:text="表4:应用二,均匀布局"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:textSize="15sp"
  android:background="#7f00ffff"/>
<TableLayout
  android:id="@+id/table4"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:stretchColumns="*"
  android:padding="3dip"
  >
  <TableRow>
   <Button android:text="一"android:layout_width="1dip"></Button>
   <Button android:text="两字"android:layout_width="1dip"></Button>
   <Button android:text="三个字"android:layout_width="1dip"></Button>
  </TableRow>
</TableLayout>
</LinearLayout>

说明:第4个TableLayout里的均匀布局的均匀效果是有限的。其有限性体现在,当该行有N列,则每列的控件内容不能多于1/N。
效果图如下:









<?xmlversion="1.0"encoding="UTF-8"?>

<LinearLayoutxmlns:android= "http://schemas.android.com/apk/res/android"

    android:orientation ="vertical"

    android:layout_width ="fill_parent"

    android:layout_height ="fill_parent"

    android:background ="#013B72">

   

    <TableLayout

       android:layout_width="fill_parent"

       android:layout_height="fill_parent"

       android:stretchColumns="1">

       <!--android:stretchColumns="1"是设置 TableLayout所有行的第二列为扩展列。 也就是说如果每行都有三列的话,剩余的空间由第二列补齐 -->  

 

          

        <TableRow>

            <TextView

                 android:text=""

                 android:layout_width="wrap_content"

                 android:layout_height="wrap_content"

                 android:textColor="#0000ff"

                />

           

            <EditText

                android:text=""

                android:id="@+id/userNumber"

                android:layout_width="fill_parent"

                android:layout_height="wrap_content"

                />

        </TableRow>

       

        <TableRow>

            <TextView

                 android:text="登录密码"

                 android:layout_width="wrap_content"

                 android:layout_height="wrap_content"

                 android:textColor="#0000ff"

                />

           

            <EditText

                android:text=""

               android:id="@+id/pwd"

                android:layout_width="fill_parent"

                android:layout_height="wrap_content"

                android:password="true"/>

        </TableRow>

       

        <TableRowandroid:gravity="right">

            <Button

                android:text="取消"

                android:id="@+id/cancleButton"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"/>

           

              <Button

                android:text="登录"

                android:id="@+id/loginButton"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"/>

             

        </TableRow>

       

    </TableLayout>

   

</LinearLayout>






 

 FrameLayout

FrameLayout(单帧布局)是最简单的一个布局对象。它被定制为你屏幕上的一个空白备用区域,之后你可以在其中填充一个单一对象— 比如,一张你要发布的图片。所有的子元素将会固定在屏幕的左上角;你不能为FrameLayout中的一个子元素指定一个位置。后一个子元素将会直接在前一个子元素之上进行覆盖填充,把它们部份或全部挡住(除非后一个子元素是透明的)


<?xml version="1.0" encoding="utf-8"?>  
<FrameLayout xmlns:android="http://schemasandroidcom/apk/res/android"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent"  
   android:orientation="vertical" >  
   <ImageView  
       android:id="@+id/img"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:contentDescription="这是一个图片"  
       android:src="@drawable/ic_launcher" />  
   <TextView  
       android:id="@+id/text"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:text="这是提示文字" />  
   <Button android:id="@+id/btn"   
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:text="这是按钮"/>  
</FrameLayout>

 

和LinearLayout类似,要在程序中控制FrameLayout就会涉及到FrameLayout类和 FrameLayout.LayoutParams类,我们就可以通过这两个类来对框架布局管理器进行控制了。下面还是先来看一下 FrameLayout.LayoutParams的文档:

 FrameLayout的作用 

今天在学习实现墨迹天气那样的拖动效果时,看到用的是重写FrameLayout。翻了翻书,突然想明白,为什么用FrameLayout.
在FrameLayout中,用我看的书中的话说是,空间永远用不完。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#897753"
    >
    <ImageView
        android:id="@+id/image1"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
        android:visibility="invisible"
       android:src="@drawable/sky"/>
    <ImageView
        android:id="@+id/image2"
        android:visibility="invisible"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
        android:src="@drawable/cloud"/>
    <ImageView
        android:id="@+id/image3"
        android:visibility="invisible"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:src="@drawable/sun"/>

</FrameLayout>

其中,image1、image2、image3都是在同一块空间的。可以说它们是重叠着的,界面显示的是最近用的那一个。
在我的代码中把它们都先不可见。
在整体代码中实现的是点一下屏幕就换一张图片。

另外,我个人感觉,实现拖动效果的关键原理就是framelayout使得几部分空间的重叠。设置只有一部分可见。当拖动时,设置其他部分移动。

我把代码也贴上,图片可以换成自己喜欢的~
FramLayoutTestActivity.java

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;

public class FramLayoutTestActivity extends Activity {
    private   String TAG ="FramLayoutTestActivity";
    private ImageView image1;
    private ImageView image2;
    private ImageView image3;
    private List<ImageView> list;

    private int count=0;


        /** Called when the activity is firstcreated. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       image1=(ImageView)findViewById(R.id.image1);
       image2=(ImageView)findViewById(R.id.image2);
       image3=(ImageView)findViewById(R.id.image3);
        list=new ArrayList<ImageView>();
        list.add(image1);
        list.add(image2);
        list.add(image3);
    }

        @Override
        public boolean onTouchEvent(MotionEventevent) {
                // TODOAuto-generated method stub
               if(event.getAction()==MotionEvent.ACTION_DOWN)
                {
                       Log.i(TAG,"move---");
                       showImage();
                }
               
                returnsuper.onTouchEvent(event);
        }
        private void showImage()
        {
               image1.setVisibility(View.VISIBLE);
               count=count%3;
                for(ImageViewi:list)
                {
                       i.setVisibility(View.INVISIBLE);
                }
               list.get(count).setVisibility(View.VISIBLE);
                count++;
        }

}
main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#897753"
    >
    <ImageView
        android:id="@+id/image1"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
        android:visibility="invisible"
       android:src="@drawable/sky"/>
    <ImageView
        android:id="@+id/image2"
        android:visibility="invisible"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:src="@drawable/cloud"/>
    <ImageView
        android:id="@+id/image3"
        android:visibility="invisible"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:src="@drawable/sun"/>

</FrameLayout>






AbsoluteLayout

 AbsoluteLayout(坐标布局)可以让子元素指定准确的x/y坐标值,并显示在屏幕上。(0, 0)为左上角,当向下或向右移动时,坐标值将变大。AbsoluteLayout没有页边框,允许元素之间互相重叠(尽管不推荐)。我们通常不推荐使用AbsoluteLayout,除非你有正当理由要使用它,因为它使界面代码太过刚性,以至于在不同的设备上可能不能很好地工作。 

  Android:layout_x/layout_y=”56px”确定控件位置 
 Android绝对布局管理器AbsoluteLayout

 有 相对布局管理器,对应的,我们还有绝对布局管理器。这和CSS中的定位布局是类似的。CSS中我们可以通过相对布局管理器对HTML元素进行布局,也可以 通过绝对布局定位通过指定top,left等属性来为元素进行布局。而在Android之中,我们还有一个绝对布局管理器可以做到同样的效果。 
    首先说明绝对布局管理器是已经在Android 2.3之后被废弃了,是不建议使用的。但绝对布局管理器却是布局管理器中最早的一种,下面来看一下绝对布局管理器的文档: 

 
    文档已经说明,这是被废弃的布局管理器,而它的继承结构为: 
java.lang.Object 
   ↳android.view.View 
   ↳android.view.ViewGroup 
   ↳android.widget.AbsoluteLayout 
    绝对布局管理器的含义就似乎采用坐标进行定位,我们的屏幕是二维结构,那么绝对布局管理器就按照x和y坐标进行定位,坐标的原点位于屏幕左上角。 
    下面在Eclipse中新建一个项目来看一下绝对布局管理器,要注意我们创建的版本为Android2.2(APILevel 8): 

Xml代码  

1.  <?xml version="1.0" encoding="utf-8"?>  

2.  <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"  

3.      android:layout_width="fill_parent"  

4.      android:layout_height="fill_parent"  

5.      android:orientation="vertical" >  

6.      <TextView  

7.          android:id="@+id/txt"  

8.          android:layout_width="wrap_content"  

9.          android:layout_height="wrap_content"  

10.        android:layout_x="40dp"  

11.        android:layout_y="10dp"  

12.        android:text="这里是一些显示信息" />  

13.</AbsoluteLayout>  


    这里我们创建了一个绝对布局管理器,其中只有一个TextView组件用于文字显示,这里我们的布局属性设置了layout_x和layout_y,也就是设置坐标,那么运行程序,我们可以看到如下效果: 

 

    那么x坐标就是40dp,y坐标是10dp,从这个坐标点开始放置我们设置的组件。下面再放置一个图片: 

Xml代码  

1.  <ImageView  

2.      android:id="@+id/img"  

3.      android:layout_width="wrap_content"  

4.      android:layout_height="wrap_content"  

5.      android:layout_x="60dp"  

6.      android:layout_y="60dp"  

7.      android:src="@drawable/ic_launcher" />  


    都是很简单的设置了,下面直接运行程序,我们可以看到如下效果: 

 

    可以看到绝对定位管理器就是根据坐标来进行布局的,这是不推荐的,因为我们无法预知运行程序的设备实际情况,如果我们定义坐标,很可能我们的程序是不能正常显示的。所以在Android 2.3之后该布局管理器就被废除了。 







0 0