Android Layout 布局 && CheckBox样例 && View三种属性 && Launcher的几点

        一个Android视图有很多控件,那么怎么来控制它们的位置排列呢?Android布局主要有以下几种:FrameLayout、LinearLayout、RelativeLayout、TableLayout、AbsoluteLayout等,FrameLayout是最简单的Layout,而最后一种AbsoluteLayout是通过指定控件的x/y坐标来定位的,因为不太灵活所以已经不推荐使用了。

        以上这些布局的关系如下:

   

        Layout中view的常用的属性主要包括:

A,Orientation方向,子控件的布局方向,可以为horizontal或vertical,如android:orientation="vertical",当然也在可以在代码里通过setOrientation()方法来设置。

B,FillMode填充方式,所有的控件都必须指定它的填充方式,即设置android:layout_width和android:layout_height,可以为三种值:(1)具体的像素值,如20px;(2)wrap_content,表示按控件文本实际长度显示;(3)fill_parent,表示填充剩下的所有可用空间。

C,Weight权重,如果你想让一行或一列的控件按比例显示,这时候权重就起到作用了。

D,layout_gravity,用来确定View在Layout中的停靠位置。

(1)LinearLayout, 实例:

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    	android:orientation="vertical" 
		android:layout_width="fill_parent" 
		android:layout_height="fill_parent">

		<LinearLayout 
			android:orientation="horizontal" 
			android:layout_width="fill_parent" 
			android:layout_height="fill_parent" 
			android:layout_weight="1">

			<TextView 
				android:text="red" 
				android:gravity="center_horizontal" 
				android:background="#aa0000" 
				android:layout_width="wrap_content" 
				android:layout_height="fill_parent" 
				android:layout_weight="1" />

			<TextView 
				android:text="green" 
				android:gravity="center_horizontal" 
				android:background="#00aa00" 
				android:layout_width="wrap_content" 
				android:layout_height="fill_parent" 
				android:layout_weight="1" />

			<TextView 
				android:text="blue" 
				android:gravity="center_horizontal" 
				android:background="#0000aa" 
				android:layout_width="wrap_content" 
				android:layout_height="fill_parent" 
				android:layout_weight="1" />

			<TextView 
				android:text="yellow" 
				android:gravity="center_horizontal" 
				android:background="#aaaa00" 
				android:layout_width="wrap_content" 
				android:layout_height="fill_parent" 
				android:layout_weight="1" />

		</LinearLayout>

		<LinearLayout 
			android:orientation="vertical" 
			android:layout_width="fill_parent" 
			android:layout_height="fill_parent" 
			android:layout_weight="1">

			<TextView
				android:text="row one" 
				android:textSize="15pt" 
				android:layout_width="fill_parent" 
				android:layout_height="wrap_content" 
				android:layout_weight="1" />

			<TextView 
				android:text="row two" 
				android:textSize="15pt" 
				android:layout_width="fill_parent" 
				android:layout_height="wrap_content" 
				android:layout_weight="1" />

			<TextView
				android:text="row three" 
				android:textSize="15pt" 
				android:layout_width="fill_parent" 
				android:layout_height="wrap_content" 
				android:layout_weight="1" />

			<TextView 
				android:text="row four" 
				android:textSize="15pt" 
				android:layout_width="fill_parent" 
				android:layout_height="wrap_content" 
				android:layout_weight="1" />

		</LinearLayout>

</LinearLayout >

      实验结果是:

                          

(2)TableLayout的行TableRow是一个横向的(horizontal)的LinearLayout

(3) RelativeLayout有16个align相关的XML属性:
        layout_alignParentBottom    当前控件低端与父控件的低端对齐(重合)
        layout_alignParentLeft        当前控件左端与父控件的左端对齐(重合)
        layout_alignParentRight      当前控件右端与父控件的右端对齐(重合)
        layout_alignParentTop        当前控件上端与父控件的上端对齐(重合)


        layout_centerHorizontal      当前控件位于父控件的横向中间位置(水平方向上的中间)
        layout_centerInParent        当前控件位于父控件的纵横向中间位置(垂直方向上的中间)
        layout_centerVertical          当前控件位于父控件的纵向中间位置(平面上的正中间)


        layout_above             使当前控件位于给出id控件的上方
        layout_below             使当前控件位于给出id控件的下方
        layout_toLeftOf          使当前控件位于给出id控件的左侧
        layout_toRightOf        使当前控件位于给出id控件的右侧


        layout_alignBottom     使当前控件与给出id控件的底部部重合(注意可用和给出id控件来对齐)
        layout_alignLeft          使当前控件与给出id控件的左边重合
        layout_alignRight        使当前控件与给出id控件的右边重合
        layout_alignTop          使当前控件与给出id控件的顶部重合
        layout_alignBaseline    使当前控件的BaseLine与给出id控件t的BaseLine重合,这个主要用于Label或者其他包含文本的widgets。
实例:

<?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"> 
    <TextView 
        android:id="@+id/label" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Type here:"/> 
    <EditText 
        android:id="@+id/entry" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:background="@android:drawable/editbox_background" 
        android:layout_below="@id/label"/><!-- have an eye on ! --> 
    <Button 
        android:id="@+id/ok" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/entry"   
       android:layout_alignParentRight="true" 
        android:layout_marginLeft="10dip" 
        android:text="OK" /> 
    <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_toLeftOf="@id/ok" 
        android:layout_alignTop="@id/ok"  
        android:text="Cancel" /> 
</RelativeLayout>

     运行结果是:

可以看出里面每个控件的设置都有使用相对布局的一些属性。很多时候需要在XML编辑和控件编辑界面之间切换,以达到满意效果。
========================================CheckBox样例===================================

       在layout的文件中,建立CheckBox的布局

    <CheckBox
        android:id="@+id/cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="46dp"
        android:layout_toRightOf="@+id/textView1"
        android:checked="false"
        android:text="male" />

在 activity中加上测试和监听语句

CheckBox cb = (CheckBox)findViewById(R.id.cb);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
	public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
		Toast.makeText(MainActivity.this, isChecked?"选中了":"取消了选中"    , Toast.LENGTH_LONG).show();
	}
});

=======================================View三种属性========================================

         View 的setVisibility有三个值可以设置:
VISIBLE--->可见
INVISIBLE--->不可见,但这个View在ViewGroupt中仍保留它的位置,不重新layout
GONE---->不可见,但这个View在ViewGroupt中不保留位置,重新layout,那后面的view就会取代他的位置。
注意:若你在Adapter中的getView或bindview将某些特殊的位置的View.INVISIBLE之后,要记得在别的判断条件下将其设置 View.Visible.不然你会看到很多怪现象的。
=======================================Launcher的几点======================================== 

文件阐述

(1)DragLayer.java:launcher layout的rootview。DragLayer实际上也是一个抽象的界面,用来处理拖动和对事件进行初步处理然后按情况分发下去,角色是一个controller。它首先用onInterceptTouchEvent(MotionEvent)来拦截所有的touch事件,如果是长按item拖动的话不把事件传下去,直接交由onTouchEvent()处理,这样就可以实现item的移动了,如果不是拖动item的话就把事件传到目标view,交有目标view的事件处理函数做相应处理。如过有要对事件的特殊需求的话可以修改onInterceptTouchEvent(MotionEvent)来实现所需要的功能。
  DragController.java:为Drag定义的一个接口。包含一个接口,两个方法和两个静态常量。接口为DragListener(包含onDragStart(),onDragEnd()两个函数),onDragStart()是在刚开始拖动的时候被调用,onDragEnd()是在拖动完成时被调用。在launcher中典型的应用是DeleteZone,在长按拖动item时调用onDragStart()显示,在拖动结束的时候onDragEnd()隐藏。两个函数包括startDrag()和setDragItemInfo().startDrag()用于在拖动是传递要拖动的item的信息以及拖动的方式,setDragItemInfo()用于传递item的参数信息(包括位置以及大小)。两个常量为DRAG_ACTION_MOVE,DRAG_ACTION_COPY来标识拖动的方式,DRAG_ACTION_MOVE为移动,表示在拖动的时候需要删除原来的item,DRAG_ACTION_COPY为复制型的拖动,表示保留被拖动的item。

(2)Workspace.java:抽象的桌面。由N个celllaout组成,从cellLayout更高一级的层面上对事件的处理

(3)LauncherProvider.java:launcher的数据库,里面存储了桌面的item的信息。在创建数据库的时候会loadFavorites(db)方法,loadFavorites()会解析xml目录下的default_workspace.xml文件,把其中的内容读出来写到数据库中,这样就做到了桌面的预制。

(4)CellLayout.java:组成workspace的view,继承自viewgroup,既是一个dragSource,又是一个dropTarget,可以将它里面的item拖出去,也可以容纳拖动过来的item。在workspace_screen里面定了一些它的view参数。

(5)ItemInfo.java:对item的抽象,所有类型item的父类,item包含的属性有id(标识item的id),cellX(在横向位置上的位置,从0开始),cellY(在纵向位置上的位置,从0开始),spanX(在横向位置上所占的单位格),spanY(在纵向位置上所占的单位格),screen(在workspace的第几屏,从0开始),itemType(item的类型,有widget,search,application等),container(item所在的)。

(6)DeleteZone:删除框。在平时是出于隐藏状态,在将item长按拖动的时候会显示出来,如果将item拖动到删除框位置时会删除item。DeleteZone实现了DropTarget和DragListener两个接口。

主要模块

(1)Launcher的界面的rootview是DragLayer,它是一个FrameLayout,在它上面workspace(应该说是celllayout)占了绝大部分的空间,celllayout的参数文件是workspace_screen.xml。workspace既是一个DropTarget又是一个DragSource

(2)将所有的应用都排列在桌面是通过首先创建一个三维的boolean型全局数组来记录item的排列情况,第一维是屏数,第二维是纵向上的排列情况,第三维是横向的排列情况,如果那个位置被item所占用就标记为1,否则标记为0。在启动时把全局数组初始化为0,然后在添加的时候把相应的位置置1。凡是涉及到workspace上item的变化,比如移动、添加、删除操作时都需要维护数组,保持数组的正确性,因为在安装新程序时依据数组的状态去判断把item加到什么位置。

(3)动态增加屏幕是通过worksapce .addchild(view)的方式实现。基本思路是:首先预先规定所允许的最大的屏幕数,然后在需要增加屏幕而且当前屏幕数没有超过最大屏幕数的时候通过(CellLayout)mInflater.inflate(R.layout.workspace_screen,null)创建一个celllayout实例出来,然后通过addchild把它加入进去。

(4)预置桌面内容

a.添加普通的应用程序快捷方式:

在../res/xml下的default_workspace.xml文件中加入默认要放置的普通的应用程序。加入的格式为:
  <favorite
  launcher:packageName="... " //应用的packageName
  launcher:className="... " //应用启动时的第一个activity
  launcher:screen="..." //放置在第几屏(放在workspace的时候需要,从0开始,0为第一屏,1为第二屏,以此类推...)
  launcher:x="..." //放置x方向的位置(在列中的位置)
  launcher:y="..." /> //放置y方向的位置(在行中的位置)
  packageName和className可以通过点击程序,然后在打印出的log中找到comp={...},例如如下信息:
  comp={com.estrongs.android.taskmanager/com.estrongs.android.taskmanager.TaskManager}。其中com.estrongs.android.taskmanager为packageName,com.estrongs.android.taskmanager.TaskManager为className。

b.添加widget:
在../package/apps/VLauncher/res/xml下的default_workspace.xml文件中加入默认要放置的普通的应用程序。加入的格式为:
  <widget
  launcher:packageName="..." //widget的packageName
  launcher:className=" ..." //实现 widget的 receiver 类的名称.
  launcher:container="..." //放置的位置(只能为desktop)
  launcher:screen="..." //放置在第几屏上
  launcher:x="..." //放置的x位置
  launcher:y="..." //放置的y位置
  launcher:spanx="..." //在x方向上所占格数
  launcher:spany="..."/> //在y方向上所占格数

c,workspace的布局如下:





参考原文:http://android.tgbus.com/Android/tutorial/201104/348551.shtml

参考原文:http://www.cnblogs.com/playing/archive/2011/04/07/2008620.html

参考原文:http://www.cnblogs.com/wt616/archive/2011/06/20/2085368.html

参考原文:http://blog.sina.com.cn/s/blog_48964b1201019g9a.html 

参考原文:http://blog.csdn.net/easy_gemini/article/details/8219968

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值