2011-05-19 612 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);