2010-11-08 293 views

回答

188

其實這是可能的,儘管什麼在其他的答案說來實現。如果您有FrameLayout,並且想要將子項目放在最下方,則可以使用android:layout_gravity="bottom",這將使該子項與FrameLayout的底部對齊。

我知道它的作品,因爲我使用它。我知道已經晚了,但它可能會來方便別人,因爲這排名前位置上的谷歌

+7

嗨byte1918,如果您認爲這不是問題的正確答案,那麼您應該非常歡迎 - 並且我認爲這對每個人都有用 - 如果您發佈了正確的解決方案。或者至少解釋是什麼讓這是一個錯誤的答案。謝謝。 – lblasa 2012-09-28 16:13:24

+14

要對齊右下角,可以使用android:layout_gravity =「bottom | right」 – 2013-09-23 00:13:04

+1

如果它不能立即反映在預覽中,請嘗試從設計選項卡中進行設置。通常工作, – yUdoDis 2015-07-15 18:31:49

-4

你不能用FrameLayout來做到這一點。

從規格:

http://developer.android.com/reference/android/widget/FrameLayout.html

「的FrameLayout被設計來阻擋屏幕顯示單個項目上的一個區域,您可以將多個孩子去的FrameLayout增加,但所有的孩子都盯住屏幕左上角。「

爲什麼不使用RelativeLayout?

+0

我想要一個列表顯示在左側,重疊地圖。哦,這可能與RelativeLayout不是嗎?好,謝謝。 – 2010-11-09 03:28:08

+1

「但所有的孩子都固定在屏幕的左上方」 - 不一定是屏幕的頂部,你可以將它對齊到右邊,底部等等嗎? – 2010-11-09 03:30:31

+0

是的,這是可能的RelativeLayout。您可以在彼此頂部佈局視圖。 – Juhani 2010-11-09 19:01:31

32

它可以通過RelativeLayout

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <ImageView 
     android:src="@drawable/icon" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentBottom="true" /> 

</RelativeLayout> 
+1

如果在'RelativeLayout'中使用'wrap_content',這是行不通的。因爲無論如何,RelativeLayout在整個屏幕上縮放。 – 2013-12-20 20:03:55

2

機器人:layout_gravity =「底」

+0

爲什麼我們使用重力,具體什麼是 – blackHawk 2017-03-04 15:09:01

40

我也遇到了這種情況,並想出瞭如何使用做FrameLayout裏。 以下輸出由以下代碼生成。

Some right-bottom aligned text showing up over an image using FrameLayout.

   <FrameLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" > 

        <ImageView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:src="@drawable/contactbook_icon" 
         android:layout_gravity="center" /> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="140" 
         android:textSize="12dp" 
         android:textColor="#FFFFFF" 
         android:layout_gravity="bottom|right" 
         android:layout_margin="15dp" /> 
       </FrameLayout> 

變化幅度值,以調整在圖像的文本位置。刪除保證金可能會使文本有時不在視圖中。

+0

最好的答案......謝謝,解決了長久以來的謎團。 – YvesLeBorg 2017-04-08 14:35:02

12

設置已android:layout_gravity="bottom|right"工作對我來說

5

兩種方法可以做到這一點:

1)使用的幀結構

android:layout_gravity="bottom|right" 

2)使用相對佈局

android:layout_alignParentRight="true" 
android:layout_alignParentBottom="true" 
0

你可以添加一個不可見的TextView到FrameLayout。

<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:visibility="invisible" 
     /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:orientation="horizontal"> 
     <Button 
      android:id="@+id/daily_delete_btn" 
      android:layout_width="0dp" 
      android:layout_height="50dp" 
      android:layout_weight="1" 
      android:text="DELETE" 
      android:layout_gravity="center"/> 
     <Button 
      android:id="@+id/daily_save_btn" 
      android:layout_width="0dp" 
      android:layout_height="50dp" 
      android:layout_weight="1" 
      android:text="SAVE" 
      android:layout_gravity="center"/> 
    </LinearLayout> 

0

如果您想嘗試使用Java代碼。你去 -

final LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, 
              LayoutParams.WRAP_CONTENT); 
    yourView.setLayoutParams(params); 
    params.gravity = Gravity.BOTTOM; // set gravity