尽管解决了其他类似问题,但我不太明白为什么这些按钮不会出现在我的布局屏幕中,有人可以解释一下原因吗?
TextView 显示正常
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.lab1.ac01220.com1032_lab.MainActivityFragment"
tools:showIn="@layout/activity_main"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_string"/>
</LinearLayout>
</LinearLayout>
最佳答案
您的 TextView
占用了所有空间,因为 match_parent
的高度和宽度。由于 LinearLayouts
是按顺序排列的,因此没有空间容纳其后的任何内容。
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
将其更改为 wrap_content
,使用 layout_weight
或特定尺寸(任何适合您的需求)。
关于android - 按钮未显示在布局中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48794731/