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

查看:1147
本文介绍了如何在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:

<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,以便您可以确定在其中添加文本的方式(有关如何操作,请参见此处的答案:

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天全站免登陆