Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.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
Java 如何将FrameLayout居中放置在RelativeLayout中_Java_Android_Android Layout_Android Fragments_Canvas - Fatal编程技术网

Java 如何将FrameLayout居中放置在RelativeLayout中

Java 如何将FrameLayout居中放置在RelativeLayout中,java,android,android-layout,android-fragments,canvas,Java,Android,Android Layout,Android Fragments,Canvas,我的布局有问题,如果一些安卓专家能检查一下并帮助我,那就太好了:) 我有一个片段a,其中包含另一个片段B FragmentA包含3个框架布局: 框架布局:应位于顶部 FrameLayout:应位于第一个的下方,包含碎片B(id=frame2) FrameLayout:应位于底部 FragmentB还包含1个框架布局: 我以编程方式将视图(例如图像)添加到此FrameLayout,方法如下: 问题< /强>是,图像不是在中间水平居中。 这是第一个片段A的布局文件: &

我的布局有问题,如果一些安卓专家能检查一下并帮助我,那就太好了:)

我有一个片段a,其中包含另一个片段B

  • FragmentA包含3个框架布局:

    • 框架布局:应位于顶部
    • FrameLayout:应位于第一个的下方,包含碎片B(id=frame2)
    • FrameLayout:应位于底部
  • FragmentB还包含1个框架布局:

    • 我以编程方式将视图(例如图像)添加到此FrameLayout,方法如下:


<强>问题< /强>是,图像不是在中间水平居中。

这是第一个片段A的布局文件:

<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>
我认为在
RelativeLayout
中使用
android:gravity=“center\u horizontal”
,在
FragmentA
FragmentB
框架布局中使用
android:layout\u width=“wrap\u content”
,图像应该居中。但它在左边

有两个屏幕截图显示它的外观(第一个)和应该的外观(第二个):


很抱歉链接,但我无法发布图片(声誉不够)

相对论您处理重力的方式可能与您预期的不同():

请注意,由于RelativeLayout认为每个子对象相对于其他子对象的位置非常重要,因此设置重力将影响所有子对象在父对象中作为单个单元的位置。这发生在孩子们被相对定位之后

快速修复方法是将frame2从FrameLayout更改为RelativeLayout,添加匹配父宽度,并将重心放在该宽度上

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

根据视图的要求,您可以采取许多其他方法。通常最好避免嵌套RelativeLayouts,但在您的情况下,这可能是必要的


可能重复。

您是否尝试了
android:layout\u centerHorizontal=“true”
内部的
RelativeLayout
?是的,我已经在RelativeLayout内部和framelayout内部尝试过了使用
canvas.drawBitmap
。您只需使用
ImageView
并将位图设置为其源。确保ImageView是
wrap\u content
。是的,我必须使用cancas,因为我在图像上画了一些线。如果您使用的是画布,则需要在
onMeasure
函数中设置自定义视图的适当大小。否则它将占据整个屏幕的长度。请参阅:
<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>
Bitmap image= BitmapFactory.decodeResource(getResources(), id);
canvas.drawBitmap(image, 0, 0, null);
    <RelativeLayout
        android:id="@+id/frame2"
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/frame1"/>