Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在FrameLayout中将元素向右对齐?_Android_Android Xml_Android Framelayout - Fatal编程技术网

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

Android 如何在FrameLayout中将元素向右对齐?,android,android-xml,android-framelayout,Android,Android Xml,Android Framelayout,我有一个包含两个视图的框架布局,第二个类似于关闭按钮(X),我想把它放在右边 我试过布局_gravity=“right”,但没有成功 这是我的布局xml: <FrameLayout android:layout_width="320dp" android:layout_height="wrap_content" android:id="@+id/layoutSmallImg"> <ImageView android:id="@+id/bigImage"

我有一个包含两个视图的框架布局,第二个类似于关闭按钮(X),我想把它放在右边

我试过布局_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>

改用a。

我建议您改用
相对值

FrameLayout设计用于在屏幕上屏蔽一个区域以显示单个项目。您可以向FrameLayout添加多个子项,并使用重力控制它们在FrameLayout中的位置。子对象绘制在堆栈中,最新添加的子对象位于顶部。框架布局的大小是其最大子级(加上填充)的大小,无论是否可见(如果框架布局的父级允许)。只有当setConsiderGoneChildrenWhenMeasuring()设置为true时,才会使用已删除的视图调整大小

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


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

  • android:paddingLeft=“250dp”

  • 使按钮大小为320dp,并添加一个自定义背景,大部分是透明的。将其设置为patch 9,以便您可以确定在其中添加文本的位置(有关如何添加文本的信息,请参见我的回答:)


要使用FrameLayout还是RelativeLayout


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

只需将您的元素放入FrameLayout中的第二个RelativeLayout即可。并使用android:layout\u 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>

阅读文档:

FrameLayout设计用于在屏幕上标出要显示的区域 单一项目。通常,FrameLayout应用于保存单个 子视图,因为在一个视图中组织子视图可能很困难 这种方式可以扩展到不同的屏幕大小,而不需要孩子 相互重叠。但是,您可以将多个子项添加到 FrameLayout并通过以下方式控制其在FrameLayout中的位置 使用android:layout\u gravity为每个孩子分配重力 属性

尝试:


可以在框架布局中的9个不同位置放置视图


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

谢谢你的回答,我知道这在relativeLayout中是可能的,我想在框架布局中对齐它,:(然后在视图中添加左边距谢谢你的回答,我知道这在relativeLayout中是可能的,我想在框架布局中对齐它,:(是的,我希望imageView右上方的imageButton,它看起来像我图像的关闭按钮,当我单击时,图像将淡出感谢您的时间,我想我会再次与RelativeLayout合作,我没有发现我对FrameLayout的满意:)感谢您的回复,我想使用FrameLayout,默认设置,FrameLayout位置视图位于FrameLayout的左上角,我想这样做,但在右上角,您也可以使用FrameLayout实现所需的功能。将ImageButton的布局设置为“右”.layout\u alignParentRight与RelativeLayout配合使用,我不知道它是否也与FrameLayout配合使用。其他人,请说明一下。谢谢,但我尝试过layout\u gravity=“right”但不起作用:)我实际上复制了你的代码,只是删除了一行,android:visibility=“Goe”。我添加了布局\u gravity=“right”.它对我来说非常好。只是我手机的屏幕尺寸没有那么大,所以我得到了一个半切图像,但我确实把它对齐了,所以它工作得很好:)是的,我知道,我看到我的逻辑足够好,可以将ImageButton对齐,但最后,我使用了RelativeLayout,它现在起作用的关键是:)谢谢你的时间;)这很有效。我相信相对版面比框架版面更贵。谢谢!这解决了RelativeLayout占据所有屏幕时的问题
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);