待续,主要讲LinearLayout和RelativeLayout

ViewStub实现单/双layout切换

在xml中调用另一个layout的xml文件,android:actionLayout="@layout/xmlname" 

在LinearLayout中,可以通过设置visibility=“Gone”不可见,但在RelativeLayout中可能会带来问题(设置了其他的view将找不到自己的位置)

此时可以通过设置android:layout_alignWithParentIfMising="true"解决与其他view的相对问题。

显示在右下角

android:layout_gravity="bottom"
android:gravity="right"
 

等间距ImageButton排布(可以通过padding调整中间间隔)

<LinearLayout>
      <LinearLayout android:weight="1">
           <ImageButton>
      </LinearLayout>      
      <LinearLayout android:weight="1">
           <ImageButton>
      </LinearLayout>     
      <LinearLayout android:weight="1">
           <ImageButton>
      </LinearLayout>
</LinearLayout>

              

ViewStub    (初态不可见,没有size)

A ViewStub is an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime. 

在CC主界面的detail里,需要通过单击来选择是否显示上部的图片,我把他们写在两个部分,

上面一个layout通过viewStub预留空间(写到下面那个一直显示的layout中,

需要设置android:inflatedId="@+id/stubInflate"属性,并且说明android:layout="@layout/your_extend_xml"

<ViewStub android:id="@+id/stub"
android:inflatedId
="@+id/subTree"
android:layout
="@layout/mySubTree"
android:layout_width
="120dip"
android:layout_height
="40dip" />

当setVisibility(int)或者inflate()被调用以后,这个inflated view才会被添加到viewStub在它parent中预留的位置。

Perform the inflation:

 ViewStub stub = (ViewStub) findViewById(R.id.stub);
     View inflated = stub.inflate();

当调用inflate()后,ViewStub会被the inflated view替换并且返回the inflated view。

这有个好处,可以使程序不许要通过额外的findViewById()得到inflated view的引用

图片+两行文字的显示:

参考:Resources/articles/layout-tricks-efficiency

仅在RelativeLayout中有效: 
在父亲布局的相对位置 
android:layout_alignParentLeft="true"     //在布局左边 
android:layout_alignParentRight="true"    //在布局右边 
android:layout_alignParentTop="true"     //在布局上面 
android:layout_alignParentBottom="true "  //在布局的下面 

在某个控件的相对位置 
android:layout_toRightOf="@id/button1"  //在控件button1的右边,不仅仅是紧靠着 
android:layout_toLeftOf="@id/button1"   //在控件button2的左边,不仅仅是紧靠着 
android:layout_below="@id/button1 "     //在控件button1下面,不仅仅是正下方 
android:layout_above=“@id/button1”   //在控件button1上面,不仅仅是正上方 

定义和某控件对奇 
android:layout_alignTop=”@id/button1”  //和控件button1上对齐 
android:layout_alignBottom=”@id/button1”  //和控件button1下对齐 
android:layout_alignLeft=”@id/button1”  //和控件button1左对齐 
android:layout_alignRight=”@id/button1”  //和控件button2右对齐 


android:layout_centerHorizontal="true"        //水平居中 
android:layout_centerVertical="true" 
android:layout_centerInParent="true" 

仅在LinearLayout中有效 
设置控件在一排或一列中所占比例值 
android:layout_weight="1"

posted on 2011-04-25 17:57  榆钱沽酒  阅读(1612)  评论(0编辑  收藏  举报