指定widget在container的相對(duì)位置,包括:android:layout_alignParentTop, android:layout_alignParentBottom, android:layout_alignParentLeft, android:layout_alignParentRight, android:layout_centerHorizontal, android:layout_centerVertical, android:layout_centerInParent,他們的值是false|true。
如果是相對(duì)其他widget的位置,可以使用:android:layout_above , android:layout_below , android:layout_toLeftOf , android:layout_toRightOf。于其他widget位置對(duì)齊,可以使用:android:layout_alignTop , android:layout_alignBottom , android:layout_alignLeft , android:layout_alignRigh , android:layout_alignBaseline , 最后一個(gè)通常用于label的對(duì)齊。語(yǔ)法格式例子:layout_toRightOf="@id/widget_a"
我們將創(chuàng)建下面的Activity,Android XML如下:
<?xml version="1.0" encoding="utf-8"?>
<!-- 我們采用RelativeLayout的布局,并設(shè)置了pad的留邊,需要注意pad屬于widget的有效范圍 -->
< RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5px"
>
<!-- textview是我們第一個(gè)基準(zhǔn)widget,我們?cè)O(shè)置了android:paddingTop="15px"。 -->
<TextView android:id=" @+id/label "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="URL:"
android:paddingTop="15px" />
<!-- 在label的右面有一edittext,填滿(mǎn)余下的空間,并和label進(jìn)行對(duì)齊 -->
<EditText android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/label"
android:layout_alignBaseline="@id/label"
/>
<!-- 在edittext的下面并對(duì)齊最右方有一個(gè)OK button -->
<Button android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignRight="@id/entry"
android:text="OK"
/>
在OK按鍵的左邊增加一個(gè)Cancel button,并對(duì)齊 -->
<Button android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="Cancel"
/>
</RelativeLayout>
textview是我們第一個(gè)基準(zhǔn)widget,我們?cè)O(shè)置了android:paddingTop="15px",否則由于后面我們按此盡心個(gè)對(duì)齊,editiew會(huì)向上移并被chip最上端。由于editview上移,也導(dǎo)致了和下面button之間的間距過(guò)大。android是根據(jù)網(wǎng)格來(lái)安排widget的位置,每個(gè)widget都有一個(gè)確定的高度并匹配網(wǎng)格,如果widget被拉高,因?yàn)榫W(wǎng)格定位的緣故,button的相對(duì)位置并不會(huì)被抬高。同樣的如果我們?cè)O(shè)置 android:paddingTop="30px",editview的位置下沉,同樣由于網(wǎng)格的緣故,下面的button不會(huì)隨著下沉,將和eidtiew的位置有所重疊,如圖所示。
然則,我們?cè)趺粗酪olabel設(shè)置15px,如果布局都需要根據(jù)這樣的經(jīng)驗(yàn)值,就相當(dāng)郁悶,另一個(gè)解決方式,就是在定義edittext之前,就將label的對(duì)應(yīng)位置根據(jù)其進(jìn)行調(diào)整,然后再定義edittext。這在1.5版本之前有問(wèn)題,我們需要設(shè)置AndroidManifest.xml,設(shè)定我們的最小運(yùn)行版本環(huán)境為2.2,我們?cè)趍anifest中設(shè)置: <uses-sdk android:minSdkVersion="8" /> 并且在default.properties文件中設(shè)置target=android-8,在Android的XML文件,處理如下:
<TextView android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="URL:"
android:layout_alignBaseline="@+id/entry"
android:layout_alignParentLeft="true"
/>
<EditText android:id="@id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/label"
android:layout_alignParentTop="true"
/>
相關(guān)鏈接: 我的Android開(kāi)發(fā)相關(guān)文章
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
