【发布时间】:2022-01-05 13:58:06
【问题描述】:
在我的 Android 应用程序中,我尝试添加一个 TextView 来为我的一些活动添加标题。但是,它似乎不起作用,因为在我的所有活动中,TextViews 都没有出现。我是开发 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:layout_marginBottom="625dp"太大了,如果您要对所有 4 个位置使用约束,那么这不是使用约束布局的正确方法,然后只是用边距移动它,这是没有意义的。而只是使用顶部约束或底部约束,然后使用左侧和/或右侧。您将很可能永远不需要高于 32 或 64 的边距 -
@a_local_nobody 非常感谢!这对我有用,不太了解如何定位边距以外的项目。现在可以使用了!
-
没问题,我很高兴看到你从约束布局开始,线性/相对绝对不是要走的路,但旧的教程仍然教给人们,这只是浪费时间。约束布局确实需要一些练习,坚持下去
标签: android xml android-layout android-xml