【问题标题】:How to center an FrameLayout in a RelativeLayout如何使 FrameLayout 在 RelativeLayout 中居中
【发布时间】:2015-09-13 18:42:15
【问题描述】:

我的布局有问题,如果有 Android 专家可以检查并帮助我,那就太好了:)

我有一个 FragmentA,其中包含另一个 FragmentB。

  • FragmentA 包含 3 个 FrameLayout:

      1. FrameLayout:应该在顶部
      1. FrameLayout:应该在第一个包含FragmentB的下方(id= frame2)
      1. FrameLayout:应该在底部
  • FragmentB 还包含 1 个 FrameLayout:

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

frameLayout.addView(new ImageClass(id);

问题是,图片不在中间水平居中。

这是第一个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>

这是fragmentB的布局文件(包含在FragmentA -> FrameLayout 2中,ID为frame2)

<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);

我认为android:gravity="center_horizontal"RelativeLayoutandroid:layout_width="wrap_content"FrameLayoutsFragmentAFragmentB 中,图像应该居中。但它在左侧。

有两个屏幕截图,它的外观(第一)和它的外观(第二):

How it looks

How it should look

对不起链接,但我不能发布图片(没有足够的声誉)

【问题讨论】:

  • 你试过android:layout_centerHorizontal="true"inside RelativeLayout吗?
  • 是的,我在 RelativeLayout 和 FrameLayout 内部都试过了
  • 使用canvas.drawBitmap有什么具体原因吗?您可以只使用ImageView 并将位图设置为它的源。确保 ImageView 是wrap_content
  • 是的,我必须使用cancas,因为我在图像上画了一些线条
  • 如果您使用的是画布,那么您需要在onMeasure 函数中设置自定义视图的适当大小。否则它将占据屏幕的整个长度。参考这个:stackoverflow.com/questions/5042197/…

标签: java android android-layout android-fragments canvas


【解决方案1】:

RelativeLayouts 处理重力的方式与您预期的不同 (link):

请注意,由于 RelativeLayout 认为每个子项相对于彼此的位置很重要,因此设置重力会影响所有子项在父项中作为单个单元的定位。这发生在孩子已经相对定位之后。

快速解决方法是将 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"/>

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

可能重复here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-26
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    相关资源
    最近更新 更多