如何在 FrameLayout 中将元素向右对齐?
我有一个包含 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我建议您使用
RelativeLayout
代替顺便说一句,您想要将按钮放在 ImageView 的顶部还是旁边?您将布局和 imageView 的宽度设置为相同的大小。
评论后,我可以看到两个选项:
android:paddingLeft="250dp"
将按钮大小设置为 320dp 并添加自定义背景,大部分是透明的。使其成为补丁 9,以便您可以确定在其中添加文本的位置(请参阅此处我的答案以了解如何执行此操作:如何创建小部件右侧没有下三角形的 Android 微调器)
I recommend you to use a
RelativeLayout
insteadBTW, 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:
android:paddingLeft="250dp"
make the button size 320dp and add a custom background, mostly transparent. Make it patch 9 so you can determine where to add the text in it (see my answer here for how to do that: How to create android spinner without down triangle on the right side of the widget)
阅读文档:
尝试:
您可以在 FrameLayout 内的 9 个不同位置放置视图
Enjoy
Read the DOCS:
Try:
You can place a view in 9 different places within a FrameLayout
Enjoy
只需将您的元素放入 FrameLayout 中的第二个 RelativeLayout 即可。并对要右对齐的元素使用 android:layout_alignParentRight="true" 。
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.
请使用 RelativeLayout 代替。
Use a RelativeLayout instead.
您可以通过编程方式使用 FrameLayout.LayoutParams 设置边距。以下内容可以将视图放置在屏幕底部:
Programatically, you can set the margin using FrameLayout.LayoutParams. The following would work to place a view on the bottom of your screen:
你想使用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.