2013-08-23 78 views
0

我應該做些什麼改變才能將相對佈局放在底部?我想將我的相對佈局放在底部

 <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="86dp" 
    android:layout_alignParentBottom="true" 
    android:layout_weight="0.08" 
    android:gravity="bottom" > 
    </RelativeLayout> 
     </LinearLayout> 
+0

刪除android:layout_weight =「0.08」,您沒有將權重指定給其父級佈局。並使父親RelativeLayout。 – Rahul

+0

@rahulkapoor'weight_sum'在使用'weight'時不是必須的.​​.....只有在某些情況下 – codeMagic

+0

其仍然沒有幫助。 –

回答

1

你需要改變你的父母佈局是一個RelativeLayout,因爲屬性android:layout_alignParentBottom無助於LinearLayout孩子:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="86dp" 
     android:layout_alignParentBottom="true" > 
    </RelativeLayout> 
</RelativeLayout> 
0

您可以通過使用FrameLayout代替LinearLayout的,因爲它在默認情況下棧孩子的從上到下實現這一目標。

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="86dp" 
     android:layout_gravity="bottom" > 
    </RelativeLayout> 

</FrameLayout> 
+0

框架佈局也沒有幫助:/ –

+0

檢查我編輯的答案。你需要「layout_gravity」才能達到目的。 – gian1200

+0

作爲父項使用框架佈局而不是相對時有什麼區別? @ gian1200 –

0

使用

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <!-- replace this by any other layout or view. You might want something here, right? -->  
    <View android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"/> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="86dp"> 
    </RelativeLayout> 

</LinearLayout> 
0

有兩個問題在這裏。首先是,

android:gravity

是錯誤的。這爲View的孩子的內容設置gravity。因此,例如在TextView中使用它,如果TextViewtext置於底部。你想,而不是什麼是

android:layout_gravity

這臺其父項內的View小號gravity

的第二個問題是,android:gravity="bottom"不工作,你會期望在一個LinearLayout誰的orientationvertical。我不確定我能解釋爲什麼,所以我會嘗試找到一個解釋它的鏈接。但是,如果您將LinearLayoutorientation更改爲horizontal,您將會看到這一點。

所以,你最好的選擇是改變orientation如果這是一個可能性,如果沒有的話根本ViewGroup更改爲RelativeLayout和使用屬性android:alignParentBottom="true"有人已經建議。

編輯的解釋

This blog postthis SO answer,等等解釋一點。但基本上,如果orientationhorizontal那麼View就像我們所期望的那樣從左向右排列,因此您無法控制左側和右側gravity。對於vertical orientation,頂部和底部gravity的情況也是如此,因爲當您設置orientation時,它們垂直放置的位置已由Android確定。我希望這是有道理的。

相關問題