【问题标题】:TextView Not centered in app but centered in match_constraintTextView 不以应用为中心,但以 match_constraint 为中心
【发布时间】:2018-03-08 22:36:03
【问题描述】:

我使用 ConstraintLayout 创建了一个简单的活动设计。 每当我尝试将 textView 居中时,它都会在蓝图中正确执行,但在实际应用程序中从未这样做。不知道是我做错了什么还是我失去了理智。

这是图片

这是 XML 代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_green"
tools:context="nz.co.listcosolutions.StartActivity">

<ImageView
    android:id="@+id/imageView4"
    android:layout_width="160dp"
    android:layout_height="163dp"
    android:layout_marginEnd="95dp"
    android:layout_marginStart="95dp"
    android:layout_marginTop="32dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/baby_plant" />

<Button
    android:id="@+id/btnNext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="32dp"
    android:layout_marginStart="32dp"
    android:layout_marginTop="64dp"
    android:text="@string/next"
    android:textColor="@color/background_green"
    android:textSize="18sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView3" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="20dp"
    android:text="Welcome to My App"
    android:textAlignment="center"
    android:textColor="@android:color/white"
    android:textSize="24sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView4" />

</android.support.constraint.ConstraintLayout>

我也在使用最新版本的 ConstraintLayout

compile 'com.android.support.constraint:constraint-layout:1.0.2'

【问题讨论】:

  • 您的 TextView 在他的属性中需要 android:gravity="center"。 (如果您指的是使文本居中)
  • 是的,文本已修复。 textAlignment 不应该做同样的事情吗?特别是因为它在蓝图中正确显示
  • 不,不是,textAlignment 它几乎没用
  • 啊奇怪。我以为它们总是基本相同的。每当我使用“textAllignment”标签时,它都非常适合我。请把你的答案作为我可以接受的正确答案。

标签: java android xml


【解决方案1】:

你需要添加:

android:gravity="center"

TextView。 这是在 TextView 对象或其子类之一中居中文本的唯一确定方法。

android:textAlignment 并非在所有情况下都有效,正如this answer 报告的那样,它在较低的 API 级别中存在问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    • 2014-07-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多