21

我有一个 FrameLayout ,其中包含 2 个视图,第二个类似于 a Close Button (X),我想将其放置在右侧。

我试过 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>
4

6 回答 6

33

我建议您改用RelativeLayouta

FrameLayout 旨在阻止屏幕上的一个区域以显示单个项目。您可以将多个子项添加到 FrameLayout 并使用重力控制它们在 FrameLayout 中的位置。孩子们被画在一个堆栈中,最近添加的孩子在最上面。框架布局的大小是其最大子项(加上填充)的大小,可见与否(如果 FrameLayout 的父项允许)。只有当 setConsiderGoneChildrenWhenMeasuring() 设置为 true 时,才会使用 GONE 的视图来调整大小。

顺便说一句,你想要 ImageView 顶部的按钮还是旁边的按钮?您将布局和 imageView 的宽度设置为相同的大小。


评论后,我可以看到两个选项:

于 2011-05-19T15:12:45.173 回答
28

阅读文档:

FrameLayout 旨在阻止屏幕上的一个区域以显示单个项目。通常,应该使用 FrameLayout 来保存单个子视图,因为很难以一种可缩放到不同屏幕尺寸的方式来组织子视图,而不会使子视图相互重叠。但是,您可以使用 android:layout_gravity 属性将多个子项添加到 FrameLayout 并通过为每个子项分配重力来控制它们在 FrameLayout 中的位置。

尝试:

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

您可以在 FrameLayout 中的 9 个不同位置放置视图

享受

于 2014-09-15T18:47:28.407 回答
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>
于 2012-12-23T13:44:13.240 回答
1

请改用相对布局。

于 2011-05-19T15:12:04.140 回答
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);
于 2016-02-08T12:20:11.903 回答
0

你想使用 FrameLayout 还是 RelativeLayout?

我的理解是,如果您想用另一个视图替换当前视图,则使用 FrameLayout。RelativeLayout 可以满足您的要求。

于 2011-05-19T15:17:08.130 回答