关于android:如何在FrameLayout中将元素右对齐?

How can I align an element to the right in the FrameLayout?

我有一个包含2个视图的FrameLayout,第二个类似于Close Button (X),我想将其放置在右侧。

我已经尝试过layout_gravity =" right",但这没有用。

这是我的布局xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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 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的宽度设置为相同的大小。

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

  • android:paddingLeft =" 250dp"

  • 使按钮大小为320dp,并添加一个自定义背景,该背景通常是透明的。使它成为补丁9,以便您可以确定在其中添加文本的方式(有关如何操作的信息,请参见此处的答案:如何在小部件右侧创建不带向下三角形的android微调器)


阅读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.

尝试:

1
2
3
<TextView
     .....
     android:layout_gravity="right" />

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

请享用


只需将您的元素放入FrameLayout到第二个RelativeLayout中即可。并使用android:layout_alignParentRight =" true"作为要右对齐的元素。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<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>

请改用RelativeLayout。


通过编程,可以使用FrameLayout.LayoutParams设置边距。以下方法可以在屏幕底部放置一个视图:

1
2
3
4
5
6
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);

您要使用FrameLayout还是RelativeLayout?

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