<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background_gradient" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<ImageButton
android:id="@+id/buttonLog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/log"
android:onClick="log" />
</RelativeLayout>
</FrameLayout>
我期待我的按鈕出現在中心屏幕的FILL_PARENT行爲不。但是,它出現在屏幕的頂部中心(即按鈕居中水平,但不垂直)。Android的 - 如預期中的RelativeLayout
在我看來,RelativeLayout的行爲就像是用「wrap_content」而不是「fill_parent」定義的。
有趣的是,如果我舉一個實際值,以我的RelativeLayout height屬性(安卓layout_height),如:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="100dp"
android:gravity="center" >
然後按鈕選擇正確的行爲(即按鈕也垂直居中) 。但我不想使用實際值。我想使用fill_parent!爲什麼它不適用於「fill_parent」?
有人知道發生了什麼嗎?
預先感謝您!
嗨rekaszeru。感謝回覆。據我所知,相對佈局會默認安排從左上角開始的孩子。然而,這就是爲什麼我在該RelativeLayout中設置了android:gravity =「center」屬性,所以它的子節點居中(但這不會發生)。你能解釋爲什麼中心屬性在這裏沒有做任何事嗎? – Tiago
請參閱此解釋:http://stackoverflow.com/a/6819801/506879另請參閱'RelativeLayout's'屬性:http://developer.android。com/reference/android/widget/RelativeLayout.LayoutParams.html沒有'gravity'屬性。 – rekaszeru
哇!沒有引力屬性...我實際上使用了Eclipse的代碼完成功能,當我點擊(Ctrl +空格鍵)時,它確實向我展示了相對佈局的引力屬性。一旦這個完成是由Google完成的(最近它被納入到了Android插件中),我期望的最少的是它是正確的......我想我不能依賴於Eclipse的建議。我會一直檢查網站上的文檔。我不得不說,這是非常惱人的,雖然...感謝您的幫助rekaszeru! – Tiago