【问题标题】:Layout gravity is not centered in custom FrameLayout View布局重力不在自定义 FrameLayout 视图中居中
【发布时间】:2021-10-30 17:23:36
【问题描述】:

我有使用自定义 XML 扩展 FrameLayout 的自定义布局。父子视图将android:layout_gravityandroid:gravity 设置为center。它看起来与布局预览中的外观完全相同,但在 App 中,TextViewProgressBar 未正确居中。它水平居中但不是垂直居中。

有什么建议吗?

这是我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/buttonParent"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center">

        <ImageView
            android:id="@+id/buttonIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:layout_gravity="center"
            android:scaleType="fitCenter"/>

        <TextView
            android:id="@+id/buttonText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textAllCaps="true"/>

    </LinearLayout>

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:indeterminate="true"
        android:layout_gravity="center"
        android:gravity="center"/>

</FrameLayout>

LayoutInspector 的输出

更新 - 解决方案:

我找到了解决方案。我必须将此添加到我的课程中,扩展 FrameLayout

val rootParams = LayoutParams(buttonWidth.toInt(), buttonHeight.toInt())
rootParams.gravity = Gravity.CENTER
layoutParams = rootParams

看起来 XML 膨胀布局中的 FrameLayout 父级不被视为父级。 Parent 在 XML 中甚至不可见,它是我膨胀 XML 的类。我必须通过this.parameter 访问父参数而不是 XML。

【问题讨论】:

  • 你的父母 FrameLayout 高度是 wrap_content 使它成为 match_parent
  • 它是包装内容,因为按钮不只是 match_parent。它可以有自定义宽度
  • 你没有把layout_gravitygravity作为center给FrameLayout
  • 将 FrameLayout 更改为 match_parent 并为 FrameLayout 添加重力,它仍然是相同的
  • 您是否将layout_gravitygravity 都作为中心?

标签: android android-layout


【解决方案1】:

android:layout_gravity="center 设置为您的FrameLayout

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="wrap_content"
    android:layout_gravity="center"
    android:layout_height="wrap_content">

或将android:layout_height="match_parent"android:layout_width="match_parent" 设置为您的FrameLayout

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/root"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

【讨论】:

    【解决方案2】:

    尝试在您的FrameLayout 中设置android:layout_height="match_parent" 而不是 android:layout_height="wrap_content",如下所示。

    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/root"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">
    ......
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-24
      • 1970-01-01
      • 1970-01-01
      • 2011-09-07
      • 1970-01-01
      • 2011-03-16
      • 1970-01-01
      相关资源
      最近更新 更多