android:layout_centerHorizo​​ntal ="true"在 android 中不起作用

标签 android layout

我正在使用此布局在中心制作图像和文本。但它不起作用。我只是在使用 android:layout_centerHorizo​​ntal="true"。为什么它不起作用我不明白。有人可以帮忙解决这个问题吗?

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffffff">


        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="70dp"
            android:background="@drawable/image_tutorial1" />

        <TextView
            android:textSize="14dp"
            android:id="@+id/title"
            android:layout_below="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="@string/info4"
            android:textColor="#000000"
            android:textStyle="bold" />

    </RelativeLayout>

最佳答案

在布局中,RelativeLayout 的宽度设置为wrap-content,它根本没有占据整个可用宽度。它的内容正在居中,但它们的父 View 正在缩小以适应它们。

为 root 尝试 fill_parent:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"

此外,带有 orientation="vertical"gravity="center_horizo​​ntal"LinearLayout 也可以更轻松地完成同样的工作。

关于android:layout_centerHorizo​​ntal ="true"在 android 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15040916/

相关文章:

c# - unity SQLite 本地数据库

Android StartActivityForResult 和 Finish(带视频)

java - 是否可能 - 来自数组值的边框布局位置?

html - 使用另一个布局选项而不是表格

android - 为什么我的 Android 应用程序在更改方向时崩溃?

android - 没有AnimationDrawable可以做逐帧动画吗?

android - 从内存安装 APK

Android保存多个复选框的状态

python - 如何在Python中使整个窗口居中?

android - 将 GridLayout 元素移动到新行以适应 Kivy 中较小的屏幕