2011-05-19 67 views
17

我有一個FrameLayout包含2個視圖,第二個像Close Button (X),我想把它放在右邊。如何將一個元素與FrameLayout中的右邊對齊?

我試過layout_gravity =「right」,但沒有奏效。

這是我的佈局XML:

<FrameLayout android:layout_width="320dp" 
    android:layout_height="wrap_content" 
    android:id="@+id/layoutSmallImg"> 

    <ImageView android:id="@+id/bigImage" 
     android:layout_width="320dp" android:layout_height="wrap_content"/> 

    <ImageView android:id="@+id/bigImage" 
     android:layout_width="320dp" android:layout_height="wrap_content"/> 

    <ImageButton android:id="@+id/closebutton" 
     android:background="@android:color/transparent" 
     android:layout_height="30dp" android:layout_marginTop="10dp" 
     android:layout_alignParentRight="true" 
     android:layout_width="30dp" android:paddingLeft="40dp" 
     android:visibility="gone" 
     android:src="@drawable/close" /> 
</FrameLayout> 

回答

28

我建議你使用一個RelativeLayout代替

FrameLayout裏被設計來阻擋的區域在屏幕上顯示一個項目。您可以將多個孩子添加到FrameLayout並使用重力控制他們在FrameLayout中的位置。孩子們被堆在一起,最近新增了一個孩子。框架佈局的大小是其最大子項的大小(加上填充),可見與否(如果FrameLayout的父級允許)。僅當setConsiderGoneChildrenWhenMeasuring()設置爲true時,纔會使用GONE視圖進行大小調整。

順便說一句,你想在ImageView頂部的按鈕或旁邊的按鈕?您將layout和imageView的寬度設置爲相同的大小。


的評論後,我可以看到兩個選項:

+0

謝謝您的回答,我知道這是可能的RelativeLayout的,我想對齊在FrameLayout裏,:( 是的,我想在頂部的ImageButton | ImageView的權利,它會看起來像的關閉按鈕我圖像,當我點擊,圖像將淡出 – Houcine 2011-05-19 15:14:31

+0

爲什麼?(10個字符) – Aleadam 2011-05-19 15:16:26

+0

看我的編輯評論 – Houcine 2011-05-19 15:20:28

2

使用RelativeLayout代替。

+2

謝謝您的回答,我知道這是可能的RelativeLayout的,我想對齊在FrameLayout裏,:( – Houcine 2011-05-19 15:14:17

+1

然後加入左旁您查看 – 2011-05-19 18:01:57

0

你想使用的FrameLayout或RelativeLayout的?

我的理解是,如果您想用另一個視圖替換當前視圖,則使用FrameLayout。您的要求可以通過RelativeLayout完成。

+0

感謝您的回覆 我想使用的FrameLayout,B默認情況下,在頂部的FrameLayout positionne意見留下的FrameLayout的,我想這樣做,但在右上, – Houcine 2011-05-19 15:19:59

+0

你也可以通過FrameLayout實現你想要的。將ImageButton的layout_gravity設置爲「正確」。 layout_alignParentRight與RelativeLayout的工作,我不知道這是否與FrameLayout裏的作品,以及。任何人,拋出這個。 – varuaa 2011-05-19 18:07:14

+0

謝謝,但我已經試過layout_gravity =「正確的」,但它沒有工作:) – Houcine 2011-05-19 20:50:07

9

只要把你的元素放在FrameLayout到第二個RelativeLayout。並使用android:layout_alignParentRight =「true」爲您要對齊的元素。

<FrameLayout android:layout_width="320dp" android:layout_height="wrap_content" 
             android:id="@+id/layoutSmallImg"> 
<RelativeLayout 
      android:id="@+id/content" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 
    <ImageView android:id="@+id/bigImage" 
      android:layout_width="320dp" 
      android:layout_height="wrap_content"/> 

    <ImageButton android:id="@+id/closebutton" 
      android:background="@android:color/transparent" 
      android:layout_height="30dp" android:layout_marginTop="10dp" 
      android:layout_alignParentRight="true" 
      android:layout_width="30dp" android:paddingLeft="40dp" 
      android:visibility="gone" 
      android:src="@drawable/close" /> 
    </RelativeLayout> 
</FrameLayout> 
13

閱讀DOCS:

的FrameLayout被設計爲阻擋的區域在屏幕上顯示 單個項目。一般來說,FrameLayout應該用於保存子視圖,因爲可以很難以 的方式組織子視圖,這種方式可以擴展到不同的屏幕尺寸,而不需要兒童 彼此重疊。你可以,但是,多個孩子添加到 的FrameLayout和 控制的FrameLayout中的位置重力分配給每個孩子,使用了android:layout_gravity 屬性。

嘗試:

<TextView 
    ..... 
    android:layout_gravity="right" /> 

您可以將9米不同的地方在一個視圖中一個的FrameLayout

享受

+0

這個作品。我相信,RelativeLayouts比的FrameLayout佈局也更加昂貴。 – 2015-07-23 17:46:45

+0

謝謝!這就解決問題時的RelativeLayout通吃屏幕https://stackoverflow.com/questions/6486214/relativelayout-is-taking-fullscreen-for-wrap-content – 2017-11-09 13:26:21

1

編程方式,您可以設置使用FrameLayout.LayoutParams保證金。以下將努力把視圖在屏幕的底部:

int desired_height_of_content = //whatever your want the height of your view to be 
FrameLayout layout = new FrameLayout(this); 
FrameLayout.LayoutParams flp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, desired_height_of_content); 
int difference = screen_height - desired_height_of_content 
flp.setMargins(difference, 0, 0, 0); 
layout.addView(your_view, flp); 
相關問題