java - Android - 布局内其他 View 中的中心 View

标签 java android xml layout

您好,我无法将 TextView 置于 ImageView 的中心。我希望它看起来有点像这样,但让它实际上垂直居中,并在字体大小发生变化时保持垂直居中。

enter image description here

这是我在 XML 中的内容,我不在乎是否需要将相对布局更改为另一种对我来说无关紧要的布局,我只是希望能够将其居中。 xml 如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.spencer.app.ProfileActivity"
android:id="@+id/layout">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minHeight="140dp"
    android:minWidth="140dp"
    android:id="@+id/logoBackground"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="25dp"
    android:layout_marginStart="25dp"
    android:layout_marginTop="25dp"
    android:contentDescription="@string/content_description_useless"
    android:background="#ffffd0cd" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/capital_u"
    android:id="@+id/u_textView"
    android:textSize="80sp"
    android:layout_marginLeft="31dp"
    android:layout_marginStart="31dp"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="20dp"
    android:layout_alignTop="@+id/logoBackground"
    android:layout_alignLeft="@+id/logoBackground"
    android:layout_alignStart="@+id/logoBackground" />

</RelativeLayout>

感谢您提前提供的帮助。

最佳答案

试试这个

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.spencer.app.ProfileActivity" >

    <ImageView
        android:id="@+id/logoBackground"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="25dp"
        android:layout_marginStart="25dp"
        android:layout_marginTop="25dp"
        android:background="#ffffd0cd"
        android:contentDescription="@string/content_description_useless"
        android:minHeight="140dp"
        android:minWidth="140dp" />

    <TextView
        android:id="@+id/u_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/logoBackground"
        android:layout_alignLeft="@+id/logoBackground"
        android:layout_alignRight="@+id/logoBackground"
        android:layout_alignStart="@+id/logoBackground"
        android:layout_alignTop="@+id/logoBackground"
        android:text="@string/capital_u"
        android:textSize="80sp"
        android:gravity="center" />

</RelativeLayout>

关于java - Android - 布局内其他 View 中的中心 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27327630/

相关文章:

android - 为什么在我创建具有抽屉导航的应用程序时看到两个工具栏?

java - 应用程序打开时推送通知不起作用?

java - 为什么 Blowfish 加密中的正斜杠会被删除?

java - Java 无法识别自定义监视器分辨率

java - Statement.executeQuery() 为 SELECT 语句返回什么

android - 如何使用 "libsu"库(或 adb)在 Android Q 上安装拆分 APK 文件?

java - 如何使用 Map 调用对象上的操作

java - 在Android Studio-3.6.2中离线安装gradle

javascript - 即时将 XML 转换为 HTML,并添加 JavaScript 事件?

xml - 如何计算 XSLT 中的引用总数?