简体   繁体   English

如何将FrameLayout放置在RelativeLayout中

[英]How to center an FrameLayout in a RelativeLayout

I've a problem with my layout and it would be great, if some Android experts could check it and help me :) 我的布局存在问题,如果一些Android专家可以检查并帮助我,那将是很好的:)

I have a FragmentA which contains another FragmentB. 我有一个FragmentA,其中包含另一个FragmentB。

  • FragmentA contains 3 FrameLayouts: FragmentA包含3个FrameLayouts:

      1. FrameLayout: Should be at the top FrameLayout:应位于顶部
      1. FrameLayout: Should be below the first an contains FragmentB (id= frame2) FrameLayout:应位于第一个包含FragmentB (id = frame2)之下
      1. FrameLayout: Should be at the bottom FrameLayout:应位于底部
  • FragmentB contains also 1 FrameLayout: FragmentB还包含1个FrameLayout:

    • I'm adding programmatically a view (eg an image) to this FrameLayout in this way: 我以这种方式以编程方式向该FrameLayout添加视图(例如图像):

frameLayout.addView(new ImageClass(id);

The problem is, that the image is not centered horizontally in the middle. 问题是图像没有在中间水平居中。

This is the layout file of the first fragmentA: 这是第一个fragmentA的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/frame1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@+id/frame2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/frame1" />

    <FrameLayout
        android:id="@+id/frame3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

And this is the layout file of the fragmentB (included in FragmentA -> FrameLayout 2 with the ID frame2) 这是fragmentB的布局文件(包含在ID为frame2的FragmentA-> FrameLayout 2中)

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/imageClass"
    android:background="#0000FF"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</FrameLayout>

And the image will be insert in the view by canvas with this code: 并使用以下代码通过画布将图像插入视图中:

Bitmap image= BitmapFactory.decodeResource(getResources(), id);
canvas.drawBitmap(image, 0, 0, null);

I thought that with android:gravity="center_horizontal" in the RelativeLayout and with android:layout_width="wrap_content" in both FrameLayouts of the FragmentA und FragmentB , the image should be centered. 我以为RelativeLayout中的android:gravity="center_horizontal"FragmentAFragmentB两个FrameLayouts中的android:layout_width="wrap_content"都应将图像居中。 But it is on the left side. 但这是在左侧。

There are two screenshots how it looks (first) and how it should look (second): 屏幕快照有两个(第一幅)和外观(第二幅):

How it looks 看起来如何

How it should look 它应该看起来如何

Sorry for the links, but I cannot post pictures (not enought reputation) 对不起,链接,但我无法发布图片(信誉不足)

RelativeLayouts handle gravity differently than you may expect ( link ): RelativeLayouts处理重力的方式与您预期的不同( 链接 ):

Note that since RelativeLayout considers the positioning of each child relative to one another to be significant, setting gravity will affect the positioning of all children as a single unit within the parent. 请注意,由于RelativeLayout认为每个孩子相对于彼此的位置都很重要,因此设置重力会影响所有孩子作为父元素内单个单元的位置。 This happens after children have been relatively positioned. 这种情况发生在孩子的位置相对较高之后。

A quick fix is to change frame2 from a FrameLayout to a RelativeLayout, add match_parent width and put your center gravity on that. 一个快速的解决方法是将frame2从FrameLayout更改为RelativeLayout,添加match_parent宽度,然后将您的中心重力放在该位置上。

    <RelativeLayout
        android:id="@+id/frame2"
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/frame1"/>

There are a number of other approaches you could take depending on the requirements for the view. 根据视图的要求,您可以采用多种其他方法。 It's generally better to avoid nesting RelativeLayouts but in your case it may be necessary. 通常最好避免嵌套RelativeLayouts,但在您的情况下可能有必要。

Possible duplicate here . 这里可能重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM