如何在 FrameLayout 中将元素向右对齐?

发布于 2024-11-08 04:10:39 字数 927 浏览 6 评论 0原文

我有一个包含 2 个视图的 FrameLayout,第二个视图类似于 关闭按钮 (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>

I have a FrameLayout which contains 2 views , the second is something like a Close Button (X) and i want to position it on the right.

I've tried layout_gravity = " right ", but that didn't work.

This is my layout 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

何以畏孤独 2024-11-15 04:10:39

我建议您使用 RelativeLayout 代替

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

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


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

I recommend you to use a RelativeLayout instead

FrameLayout is designed to block out an area on the screen to display a single item. You can add multiple children to a FrameLayout and control their position within the FrameLayout using gravity. Children are drawn in a stack, with the most recently added child on top. The size of the frame layout is the size of its largest child (plus padding), visible or not (if the FrameLayout's parent permits). Views that are GONE are used for sizing only if setConsiderGoneChildrenWhenMeasuring() is set to true.

BTW, do you want the button on top of the ImageView or next to it? You're setting the width of both the layout and the imageView to the same size.


After the comment, I can see two options:

习惯成性 2024-11-15 04:10:39

阅读文档:

FrameLayout 的设计目的是在屏幕上遮挡一个区域来显示
单个项目。一般来说,FrameLayout应该用来容纳单个
子视图,因为在一个子视图中组织子视图可能很困难
无需孩子即可扩展到不同屏幕尺寸的方式
相互重叠。但是,您可以将多个子项添加到一个
FrameLayout 并通过以下方式控制它们在 FrameLayout 中的位置
使用 android:layout_gravity 为每个孩子分配重力
属性。

尝试:

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

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

Enjoy

Read the DOCS:

FrameLayout is designed to block out an area on the screen to display
a single item. Generally, FrameLayout should be used to hold a single
child view, because it can be difficult to organize child views in a
way that's scalable to different screen sizes without the children
overlapping each other. You can, however, add multiple children to a
FrameLayout and control their position within the FrameLayout by
assigning gravity to each child, using the android:layout_gravity
attribute.

Try:

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

You can place a view in 9 different places within a FrameLayout

Enjoy

随心而道 2024-11-15 04:10:39

只需将您的元素放入 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>

Just put your elements inside FrameLayout to the second RelativeLayout. And use android:layout_alignParentRight="true" for the element which you want to align right.

<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>
窗影残 2024-11-15 04:10:39

请使用 RelativeLayout 代替。

Use a RelativeLayout instead.

给我一枪 2024-11-15 04:10:39

您可以通过编程方式使用 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);

Programatically, you can set the margin using FrameLayout.LayoutParams. The following would work to place a view on the bottom of your screen:

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);
一世旳自豪 2024-11-15 04:10:39

你想使用FrameLayout还是RelativeLayout?

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

Do you want to use FrameLayout or RelativeLayout?

My understanding says that FrameLayout is used if you want to replace your current view with another view. Your requirement can be fulfilled by RelativeLayout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文