如何在 FrameLayout 中将元素向右对齐? [英] How can I align an element to the right in the FrameLayout?

查看:51
本文介绍了如何在 FrameLayout 中将元素向右对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 2 个视图的 FrameLayout,第二个类似于 Close Button (X),我想把它放在右边.

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.

我试过 layout_gravity = " right ",但没用.

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

这是我的布局 xml :

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>

推荐答案

我建议你改用 RelativeLayout

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

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.

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

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.

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

  • android:paddingLeft="250dp"

  • android:paddingLeft="250dp"

使按钮大小为 320dp 并添加自定义背景,大部分是透明的.将其设为补丁 9,以便您可以确定在其中添加文本的位置(有关如何执行此操作,请参阅我的答案:如何在小部件的右侧创建没有向下三角形的android微调器)

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 中将元素向右对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆