【Code-Snippet】經常使用佈局

1. RelativeLayout

經常使用屬性

第一類:屬性值爲true或false,若是對應的兄弟元素找不到的話就以父元素作參照物android

android:layout_centerHrizontal                  水平居中
    android:layout_centerVertical                   垂直居中
    android:layout_centerInparent                   相對於父元素徹底居中
    android:layout_alignParentBottom                貼緊父元素的下邊緣
    android:layout_alignParentLeft                  貼緊父元素的左邊緣
    android:layout_alignParentRight                 貼緊父元素的右邊緣
    android:layout_alignParentTop                   貼緊父元素的上邊緣
    android:layout_alignWithParentIfMissing
複製代碼

Java:bash

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, 
        RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);  //居中
複製代碼

第二類:屬性值必須爲id的引用名「@id/id-name」佈局

android:layout_below                          	在某元素的下方
    android:layout_above                          	在某元素的的上方
    android:layout_toLeftOf                       	在某元素的左邊
    android:layout_toRightOf                     	在某元素的右邊
    android:layout_alignTop                      	本元素的上邊緣和某元素的的上邊緣對齊
    android:layout_alignLeft                      	本元素的左邊緣和某元素的的左邊緣對齊
    android:layout_alignBottom                 		本元素的下邊緣和某元素的的下邊緣對齊
    android:layout_alignRight                    	本元素的右邊緣和某元素的的右邊緣對齊
複製代碼

第三類:屬性值爲具體的像素值,如30dip,40pxspa

android:layout_marginBottom              		離某元素底邊緣的距離
    android:layout_marginLeft                   	離某元素左邊緣的距離
    android:layout_marginRight                 		離某元素右邊緣的距離
    android:layout_marginTop                   		離某元素上邊緣的距離
複製代碼

代碼屬性設置:在代碼中,須要經過 Layout 的 LayoutParams去設置某個 View 的佈局參數,獲得某個 View 的佈局參數:(其必須強制轉換成XML中的對應的佈局)code

RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) 
        voice_time.getLayoutParams();
lp.addRule(RelativeLayout.LEFT_OF, R.id.bq_view);
lp.setMargins(0,0,ScreenUtils.dip2px(context, 3),0);
voice_time.setLayoutParams(lp);	     
複製代碼

addRule: 設置依靠 RelativeLayout.LEFT_OF :在 voice_time 的 left of 是 R.id.bq_view,就是 voice_time 的右邊是 R.id.bq_view,其餘相似cdn

setMargins: 設置 Margins,參數:左上右下,單位是 pxxml

2. GridLayout

GridLayout網格佈局。先指定行和列,其內的控件便自動換行排列blog

android:orientation="horizontal"	    //	指定豎向仍是橫向排列
android:rowCount="4"					//	行
android:columnCount="4"					//	列
複製代碼

子控件:ip

android:layout_rowWeight 設置子控件行的比重
android:layout_columnWeight 設置子控件列的比重
android:layout_row :  固定顯示在第幾行。 
android:layout_column :  固定顯示在第幾列,前面幾列沒控件的話就空着。 
android:layout_rowSpan : 跨幾行 
android:layout_columnSpan:  跨幾列   
複製代碼

例如一個計算器佈局能夠用 gridLayout:get

<GridLayout android:layout_weight="1"
    android:orientation="horizontal"
    android:rowCount="4"
    android:columnCount="4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <Button
        android:layout_margin="10px"
        android:id="@+id/key_number_7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="7"
        android:textSize="30pt"/>
    <Button
        android:layout_margin="10px"
        android:id="@+id/key_number_8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="8"
        android:textSize="30pt"/>
    <Button
        android:layout_margin="10px"
        android:id="@+id/key_number_9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="9"
        android:textSize="30pt"/>
    <Button
        android:layout_margin="10px"
        android:id="@+id/key_number_chu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="÷"
        android:textSize="30pt"/>
    <Button
        android:layout_margin="10px"
        android:id="@+id/key_number_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="4"
        android:textSize="30pt"/>
    <Button
        android:layout_margin="10px"
        android:id="@+id/key_number_5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="5"
        android:textSize="30pt"/>
    <Button
        android:layout_margin="10px"
        android:id="@+id/key_number_6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="6"
        android:textSize="30pt"/>
    <Button
        android:layout_margin="10px"
        android:id="@+id/key_number_cheng"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="×"
        android:textSize="30pt"/>
</GridLayout>
複製代碼

相關文章
相關標籤/搜索