指定widget在container的相對位置,包括:android:layout_alignParentTop, android:layout_alignParentBottom, android:layout_alignParentLeft, android:layout_alignParentRight, android:layout_centerHorizontal, android:layout_centerVertical, android:layout_centerInParent,他們的值是false|true。
如果是相對其他widget的位置,可以使用:android:layout_above , android:layout_below , android:layout_toLeftOf , android:layout_toRightOf。于其他widget位置對齊,可以使用:android:layout_alignTop , android:layout_alignBottom , android:layout_alignLeft , android:layout_alignRigh , android:layout_alignBaseline , 最后一個通常用于label的對齊。語法格式例子:layout_toRightOf="@id/widget_a"
我們將創建下面的Activity,Android XML如下:
<?xml version="1.0" encoding="utf-8"?>
<!-- 我們采用RelativeLayout的布局,并設置了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是我們第一個基準widget,我們設置了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,填滿余下的空間,并和label進行對齊 -->
<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的下面并對齊最右方有一個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按鍵的左邊增加一個Cancel button,并對齊 -->
<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是我們第一個基準widget,我們設置了android:paddingTop="15px",否則由于后面我們按此盡心個對齊,editiew會向上移并被chip最上端。由于editview上移,也導致了和下面button之間的間距過大。android是根據網格來安排widget的位置,每個widget都有一個確定的高度并匹配網格,如果widget被拉高,因為網格定位的緣故,button的相對位置并不會被抬高。同樣的如果我們設置 android:paddingTop="30px",editview的位置下沉,同樣由于網格的緣故,下面的button不會隨著下沉,將和eidtiew的位置有所重疊,如圖所示。
然則,我們怎么知道要給label設置15px,如果布局都需要根據這樣的經驗值,就相當郁悶,另一個解決方式,就是在定義edittext之前,就將label的對應位置根據其進行調整,然后再定義edittext。這在1.5版本之前有問題,我們需要設置AndroidManifest.xml,設定我們的最小運行版本環境為2.2,我們在manifest中設置: <uses-sdk android:minSdkVersion="8" /> 并且在default.properties文件中設置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"
/>
相關鏈接: 我的Android開發相關文章
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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