TextView 在 Android 中不显示

问题描述 投票:0回答:4

在我的 Android 应用程序中,我尝试添加 TextView 来为我的某些活动添加标题。但是,它似乎不起作用,因为在我的所有活动中,TextView 都没有显示。我是开发 Android 应用程序的新手,所以我可能会做一些根本上错误的事情,但我尝试添加不同的元素(按钮、纯文本……)并且它们都出现了,我只有 Textview 存在问题.

这是我的代码:

<androidx.constraintlayout.widget.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"
    tools:context=".WishlistActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="173dp"
        android:layout_marginTop="87dp"
        android:layout_marginEnd="156dp"
        android:layout_marginBottom="625dp"
        android:text="Wishlist stuff"
        android:gravity="center_horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    
</androidx.constraintlayout.widget.ConstraintLayout>```
android xml android-layout android-xml
4个回答
0
投票
    <?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="173dp"
        android:layout_marginTop="87dp"
        android:layout_marginEnd="156dp"
        android:layout_marginBottom="625dp"
        android:text="Wishlist stuff"
        android:gravity="center_horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

输出图像

代码没有错误,只需根据您的屏幕尺寸修正边距即可。


0
投票

删除 Textview 内的边距并尝试一下


0
投票

您可能只是将文本视图拖放到 ContraintLayout 中并将其移动到特定位置。 Android Studio 通过相应调整边距来移动视图。 然后你将它设置为 0dp,希望它能够扩展,通常情况下它会这样做。您只是错过了事后删除或调整边距的机会。

试试这个:

    <TextView
    android:id="@+id/textView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:text="Wishlist stuff"
    android:gravity="center_horizontal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

0
投票

android:layout_width =“0dp” 机器人:layout_height =“0dp”

问题出在上面的代码上。尝试使用“wrap_content”或“match_parent

© www.soinside.com 2019 - 2024. All rights reserved.