安卓速记1--布局

布局类别与名称意义

1.布局类别

Linear Layout:线性布局 Relative Layout:相对布局 

Table Layout:表格布局 FrameLayout :框架布局

AbsoluteLayout :绝对布局GridView:网格布局 

Tab Layout:选项卡布局 List View:列表布局

 

2.参数设定

layout_margin是控件边缘相对于父控件的边距

layout_padding是控件内容相对于控件边缘的边距

wrap_content填满父控件的空白

fill_parent表示大小刚好足够显示当前控件里的内容

match_parent在Android2.2中启动match_parent,不用fill_parent

²  Linear Layout (线性布局)

layout_gravity 相对于父元素的重力值(默认top|left):

(top|bottom|left|right|center_vertical|fill_vertical|center_ horizontal

|fill_ horizontal | center| fill)

LinearLayout有两个非常相似的属性:

 android:gravity

 android:layout_gravity

他们的区别在于:

android:gravity属性是对该view中内容的限定.比如一个button上面的text. 你可以设置该text相对于view的靠左,靠右等位置.

android:layout_gravity是用来设置该view相对与父view的位置.比如一个button在linearlayout里,你想把该button放在linearlayout里靠左、靠右等位置就可以通过该属性设置.

android:gravity用于设置View中内容相对于View组件的对齐方式,而android:layout_gravity用于设置View组件相对于Container的对齐方式。

²  Relative Layout (相对布局)

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

LayoutParams中特殊的参数 :

< 1>属性值为true或false

android:layout_centerHrizontal            水平居中

android:layout_centerVertical             垂直居中

android:layout_centerInparent             相对于父元素完全居中

android:layout_alignParentBottom          贴紧父元素的下边缘

android:layout_alignParentLeft            贴紧父元素的左边缘

android:layout_alignParentRight           贴紧父元素的右边缘

android:layout_alignParentTop             贴紧父元素的上边缘

android:layout_alignWithParentIfMissing   若找不到兄弟元素以父元素做参照物

< 2>属性值必须为id的引用名”@id/id-name”

android:layout_below               在某元素的下方

android:layout_above               在某元素的上方

android:layout_toLeftOf            在某元素的左边

android:layout_toRightOf           在某元素的右边

android:layout_alignBaseLine       该控件的baseline和给定ID的控件的Baseline对齐

android:layout_alignTop            本元素的上边缘和某元素的的上边缘对齐

android:layout_alignLeft           本元素的左边缘和某元素的的左边缘对齐

android:layout_alignBottom         本元素的下边缘和某元素的的下边缘对齐

android:layout_alignRight          本元素的右边缘和某元素的的右边缘对齐

< 3>属性值为具体的像素值,如30dip,40px

android:layout_marginBottom        离某元素底边缘的距离

android:layout_marginLeft          离某元素左边缘的距离

android:layout_marginRight         离某元素右边缘的距离

android:layout_marginTop           离某元素上边缘的距离

²  TableLayout (表格布局)

TableLayout 将子元素的位置分配到行或列中。一个TableLayout由许多的TableRow 组成,每个TableRow 都会定义一个 row(事实上,你可以定义其它的子对象)。

TableLayout 容器不会显示row 、cloumns 或cell 的边框线。每个row拥有0个或多个的cell;每个cell 拥有一个View对象。

表格由列和行组成许多的单元格。表格允许单元格为空。单元格不能跨列,这与HTML 中的不一样。

特殊的参数:

android:stretchColumns  伸展的列的索引

android:shrinkColumns   收缩的列的索引

android:collapseColumns 倒塌的列的索引(即销毁)

表示两行两列的一个表格。 android:gravity=”center”书面解释是权重比。其实就是让它居中显示。

它还可以动态添加里面的每行每列。如下代码所示:

TableLayout tableLayout = (TableLayout)findViewById(R.id.table01);

TableRow tableRow = new TableRow(this);

TextView temp = new TextView(this);

temp.setText("text的值");

tableRow.addView(temp);

android:stretchColumns=”1,2,3,4″ 它的意思就是自动拉伸1,2,3,4列。

²  AbsoluteLayout (绝对布局)

AbsoluteLayout可以让子元素指定准确的x/y坐标值,并显示在屏幕上。(0,0)为左上角,当向下或向右移动时,坐标值将变大。

AbsoluteLayout没有页边框,允许元素之间互相重叠(尽管不推荐)。我们通常不推荐使用

AbsoluteLayout,除非你有正当理由要使用它,因为它使界面代码太过刚性,以至于在不同的设备上可能不能很好地工作。

LayoutParams中特殊的参数 :

layout_x  x方向的坐标

layout_y  y方向的坐标

 

²  FrameLayout ( 帧布局 )

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

里面可以放多个控件,不过控件的位置都是相对位置

LayoutParams中特殊的参数 :

layout_gravity 相对于父元素的重力值(用法同LinearLayout)

注意事项:

各布局不要乱用各自的属性。比如把属于AbsoluteLayout布局的android:layout_x和android:layout_y用到LinearLayout布局或RelativeLayout布局,或者把RelativeLayout布局的 below,rightof等属性应用到其他布局中。这样做虽然不会报错,但是根本达不到我们需要的效果。

关于android:layout_width=”fill_parent”和android:layout_height=”wrap_content”,这是对每个布局宽和高的设置。wrap_content可表示随着其中控件的不同而改变这个布局的宽度或高度,类似于自动设置宽和高,fill_parent使布局填充整个屏幕,另外还有一种match_parent,它本质上和 fill_parent 一样,并从API Level8开始替代fill_parent。

 

布局复用

1、活用include

例如在layout中创建一个名为include_title.xml的文件。然后在需要添加标题的xml文件中加上 <include layout="@layout/include_title" />这话便可将标题显示在当前页面中,
例如:  

<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
    android:layout_height="match_parent"
   android:orientation="vertical" >
 
   <include layout="@layout/include_title" />
</LinearLayout>


 

2、使用extends

在一些页面中,我们总是需要共用一些页面显示效果的功能,例如Toast,Dialog等等。在这时,我们可以将这些东西封装到一个Activity中,当某个Activity需要使用里面的功能的时候,只要直接去继承它们,然后调用相应的方法即可。以下为制作一个BaseActivity的例子代码:

import android.app.Activity;
import android.widget.Toast;
public class BaseActivity extends Activity{
         /**
          * 弹出提示(int)
          *
          * @param intContent
          */
         public void showToast(int intContent) {
                   Toast.makeText(getApplication(),intContent, Toast.LENGTH_SHORT).show();
         }
        
         /**
          * 弹出提示(String)
          *
          * @param intContent
          */
         public void showToast(String strContent) {
                   Toast.makeText(getApplication(),strContent, Toast.LENGTH_SHORT).show();
         }
}


 

当我们需要使用Toast的时候,只要继承这个BaseActivity这个类,然后直接调用showToast(参数)方法便可以直接弹出Toast,是不是简单一些呢。

 

 

3、类的封装

在2中讲的是将页面显示的效果封装起来,而这里讲的是将功能代码封装起来。在一些时候,我们需要重复调用一个功能方法,是不是觉得复制粘贴很麻烦呢,在这时,我们只需要将其功能代码封装起来,供以后调用。这也就是MVC模式中的Model层。例如:

 

我们新建一个名为Tools的Java类,其代码为:

/**
 * 工具类
 *
 */
public class Tools {
 
         public static void outPut(Object obj) {
                  
                   System.out.println(obj);
                  
         }
}

在这时,假如我们想输出一个数据的时候,直接调用Tools.outPut(参数)方法便可输出数据。

 

4、使用string.xmlcolor.xml

开发一个APP的时候,我们难免会使用到一些颜色值或者文字,在这时,我们应该将其放在相对应的color.xml或string.xml文件中,这样不仅提高代码的复用性,而且也便于修改,而不用到时改点需求的时候,就要到处找出页面修改其颜色值和文字。

例如:color.xml文件

<?xml version="1.0"encoding="utf-8"?>
<resources>
 
   <color name="transparent">#00000000</color>
   <color name="black">#000000</color>
   <color name="blue">#5DC0F8</color>
 
</resources>


 

使用的时候:

   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="标题"
       android:textColor="@color/blue" />

string.xml的就不举例了,估计大家都懂了。


5、使用library

当做项目做多的时候,就会发现,其实,很多功能或者效果什么的,都非常相似,在这时,我们就该收集一下那些基本代码,将其封装在一个library中,等到用时直接加载这个library,而不需要重新写。其实就是和导入开源框架一样。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值