安卓介面布局

來源:互聯網
上載者:User

標籤:android   使用   io   ar   代碼   html   line   new   htm   

布局:
在 android 中我們常用的布局方式有這麼幾種:
1.LinearLayout ( 線性布局 ) :(裡面只可以有一個控制項,並且不能設計這個控制項的位置,控制項會放到左上方)
線性布局分為水平線性和垂直線性二者的屬性分別為: android:orientation= " horizontal " android:orientation= "vertical" 。
2.RelativeLayout ( 相對布局 ) : (裡面可以放多個控制項,但是一行只能放一個控制項)
附加幾類 RelativeLayout 的屬性供大家參考:
第一類 : 屬性值為 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 若找不到兄弟元素以父元素做參照物

第二類:屬性值必須為 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 , 40px
android:layout_marginBottom 離某元素底邊緣的距離
android:layout_marginLeft 離某元素左邊緣的距離
android:layout_marginRight 離某元素右邊緣的距離
android:layout_marginTop 離某元素上邊緣的距離
3.TableLayout ( 表格版面配置 ) : (這個要和TableRow配合使用,很像html裡面的table)
這個表格版面配置不像HTML中的表格那樣靈活,只能通過 TableRow 屬性來控制它的行而列的話裡面有幾個控制項就是幾列(一般情況)。 如:
<TableLayout>
<TableRow>
<EditText></EditText>
<EditText></EditText>
</TableRow>
<TableRow>
<EditText></EditText>
<EditText></EditText>
</TableRow>
</TableLayout>
表示兩行兩列的一個表格。
android:gravity="center" 書面解釋是權重比。其時就是讓它置中顯示。它還可以動態添加裡面的每行每列。如下代碼所示:
/*根據id尋找表格對象*/
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列。

4.AbsoluteLayout ( 絕對布局 ) : (裡面可以放多個控制項,並且可以自己定義控制項的x,y的位置)
5.FrameLayout ( 幀布局 ) :(裡面可以放多個控制項,不過控制項的位置都是相對位置)
在它裡面的控制項都是按後面的一個控制項疊加在前一個控制項上來顯示的,所有元素都被放置在最左上方。 如:
<FrameLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1">
<ImageView android:id="@+id/iv1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:visibility="invisible"
android:src="@drawable/lotusleaf"></ImageView>
<ImageView android:id="@+id/f1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/frog_right"
android:visibility="invisible"></ImageView>
</FrameLayout>
表示的是id為f1的控制項疊加在id為iv1的控制項上面顯示

(LinearLayout 和 RelativeLayout 應該又是其中用的較多的兩種。AbsoluteLayout 比較少用,因為它是按螢幕的絕對位置來布局的如果螢幕大小發生改變的話控制項的位置也發生了改變。這個就相當於HTML中的絕對布局一樣,一般不推薦使用 )

安卓介面布局

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.