Android UI学习 - Linear Layout, RelativeLayout

来源:互联网 发布:js统计当前在线人数 编辑:程序博客网 时间:2024/03/29 23:48

1. 一些常用的公共属性介绍

1) layout_width - 宽
    fill_parent: 宽度和父元素相同,wrap_content: 宽度随本身的内容所调整,或者指定 px 值来设置宽
 
2) layout_height - 高
    fill_parent: 高度和父元素相同,wrap_content: 高度随本身的内容所调整,或者指定 px 值来设置高
 
3) background - 设置背景图
 
4) padding - 设置边距
    可以具体设置paddingBottom,paddingLeft,paddingRight,paddingTop来设定不同的px值
 
5) id - 该object的id号
    @+id/id1 代表添加新的id名为id1, @id/id1 代表引用id1的控件
 
6) layout_weight - 重要度
     个人理解为显示的优先级。默认为0(最高),数值越大,优先级越低!参考下面的Linear Layout例子。要让layout_weight生效,需要父层或父父层的相应layout_width/layout_height = "fill_parent",否则wrap_content会压缩到最小足够空间!
 
7) layout_gravity - Container组件的对齐方式
     组件在layout里面的对齐方式。
 
8) gravity - 文字在组件里的对齐方式
    例如设置button里面的文字在button中居中显示。
 
* 大多数属性是可以调用对应的函数来动态改变状态的,请查看SDK Doc。
 

2. Linear Layout 线形布局

     orientation - 容器内元素的排列方式。vertical: 子元素们垂直排列,horizontal: 子元素们水平排列。在代码里可通过setOrientation()进行动态改变,值分别为HORIZONTAL或者VERTICAL。
     * 在Linear Layout,  宽度/高度都是按着组件的次序逐个占用的!所以当某个组件设置"fill_parent",在没有设置Layout_weight的情况下,该组件会占用了余下的空间,那么在它后面的组件就会显示不出来。如下图的EditText如果没有设置android:layout_weight="1", 它下面的其他组件就看不见了!
 
     baselineAligned 一般情况下,这个属性默认为true,代表在同一方向的组件都基于第一个组件对齐。所以可以看到下图的text1, button1, text2是在同一水平线的。当不需要这效果时,可以设置为false。
 
    可以参考官方网页http://androidappdocs.appspot.com/resources/tutorials/views/hello-linearlayout.html。

LinearLayout

 xml代码:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" > 
  6.  
  7.     <TextView 
  8.         android:text="@string/hello" 
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         /> 
  12.     <EditText 
  13.         android:layout_width="fill_parent" 
  14.         android:layout_height="wrap_content" 
  15.         android:layout_weight="1" 
  16.         android:id="@+id/edittext" 
  17.         /> 
  18.  
  19.     <LinearLayout 
  20.         android:id="@+id/LinearLayout01"  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:orientation="horizontal"> 
  24.         <TextView  
  25.             android:text="text1"  
  26.             android:id="@+id/TextView01"  
  27.             android:layout_width="wrap_content"  
  28.             android:layout_height="wrap_content" 
  29.             /> 
  30.         <Button android:text="Button01"  
  31.             android:id="@+id/Button01"  
  32.             android:layout_width="fill_parent"  
  33.             android:layout_height="wrap_content" 
  34.             android:layout_weight="1" 
  35.             /> 
  36.         <TextView  
  37.             android:text="text2"  
  38.             android:id="@+id/TextView02"  
  39.             android:layout_width="wrap_content"  
  40.             android:layout_height="wrap_content" 
  41.             /> 
  42.     </LinearLayout> 
  43.  
  44.     <TextView 
  45.         android:layout_width="wrap_content" 
  46.         android:layout_height="wrap_content" 
  47.         android:text="buttom" 
  48.         /> 
  49.  
  50. </LinearLayout> 
 

3. RelativeLayout  相对定位布局

     这个布局比较易懂,但组件间容易存在依赖关系,“牵一发而动全身“,所以在确定组件间布局关系不会再变动时,可以考虑采用!先看看xml代码:
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <RelativeLayout 
  3.   xmlns:android="http://schemas.android.com/apk/res/android" 
  4.   android:layout_width="wrap_content" 
  5.   android:layout_height="wrap_content" 
  6.   android:id="@+id/relativelayout"> 
  7.  
  8.     <ImageView 
  9.         android:id="@+id/image" 
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content" 
  12.         android:src="@drawable/icon" 
  13.         /> 
  14.     <TextView 
  15.         android:id="@+id/text1" 
  16.         android:layout_width="fill_parent" 
  17.         android:layout_height="wrap_content" 
  18.         android:text="@string/hello" 
  19.         android:layout_toRightOf="@id/image" 
  20.         /> 
  21.     <Button 
  22.         android:id="@+id/button1" 
  23.         android:layout_width="fill_parent" 
  24.         android:layout_height="wrap_content" 
  25.         android:text="button1" 
  26.         android:layout_toRightOf="@id/image" 
  27.         android:layout_below="@id/text1" 
  28.         /> 
  29. </RelativeLayout> 
Java代码(动态添加组件):
  1. public class RelativeDemo extends Activity { 
  2.  
  3.     @Override 
  4.     public void onCreate(Bundle savedInstanceState) { 
  5.         super.onCreate(savedInstanceState); 
  6.         setContentView(R.layout.relative); 
  7.  
  8.         RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( 
  9.             ViewGroup.LayoutParams.FILL_PARENT, //width 
  10.             ViewGroup.LayoutParams.WRAP_CONTENT //height 
  11.         ); 
  12.  
  13.         //设置editText layout_below="@id/button1" 
  14.         lp.addRule(RelativeLayout.BELOW, R.id.button1); 
  15.  
  16.         //设置editText layout_alignLeft="@id/image" 
  17.         lp.addRule(RelativeLayout.ALIGN_LEFT, R.id.image); 
  18.  
  19.         ((RelativeLayout) findViewById(R.id.relativelayout)).addView(
  20.         new EditText(this), lp); 
  21.     } 
效果图:

RelativeLayout

     先添加参照物(ImageView),然后就可以依次添加其他组件,定义位置规则rule!位置规则是不分先后的!另外ADT插件提供的预览图,有时候是不准的,未能及时更新,所以最好还是要到模拟器上测试!
 
     RelativeLayout的xml属性很多,总的来说分为2类:
     1) 要指定参照物的,layout_alignBottom,layout_toLeftOf,layout_above,layout_alignBaseline系列的;
          layout_above = ”@id/text1“
     2) 以parent为参照物,设置true/false,layout_centerVertical,layout_alignParentLeft系列的。
          layout_alignParentLeft = ”true“
 
      其中layout_alignWithParentIfMissing是比较有用且要注意的属性,当设置为true,在指定的参照物找不到的情况下,会使用parent作为新的参照物!
 
      RelativeLayout.LayoutParams是用于设置位置规则的。上述的xml属性均来自此静态类。但它的AddRule(int verb, int anchor),参数的verb动作却是引用RelativeLayout的常量,而这些常量和部分xml属性相对应。参数anchor的值可能是参照物的id,RelativeLayout.TRUE,-1(当不需要指定anchor的verb)。可以这样理解verb和anchor:
xml属性 (verb) = "value" (anchor)
     其中它的构造函数之一: RelativeLayout.LayoutParams(int w, int h),参数指定所要设置子View的宽度和高度。

本文出自 “学习Android” 博客,请务必保留此出处http://android.blog.51cto.com/268543/298345


原创粉丝点击