Android 按钮未显示在布局中

Android 按钮未显示在布局中,android,xml,android-layout,Android,Xml,Android Layout,尽管提出了其他类似的问题,但我不太明白为什么这些按钮不会出现在我的布局屏幕上,有人能解释一下原因吗 文本视图显示得很好 <?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:too

尽管提出了其他类似的问题,但我不太明白为什么这些按钮不会出现在我的布局屏幕上,有人能解释一下原因吗

文本视图显示得很好

<?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>


这是一个简单的修复程序。将设备旋转到“水平”位置,您可以看到按钮。

您的
文本视图
占用了所有空间,高度和宽度与
匹配。由于
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\u content
,使用
layout\u weight
,或使用特定尺寸(根据您的需要)。

您仍然可以将
TextView
设置为
匹配父项。但请记住设置
边距
布局权重

请看下面我的代码片段

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:layout_margin="10dp"

    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:layout_gravity="center"
        android:text="Hello World!"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:text="Submit"/>

</LinearLayout>


请发布您的UI屏幕快照。此外,请发布活动或片段的Java代码。您的
文本视图
占用了所有空间。尝试将其高度和宽度更改为
wrap\u content
,然后查看发生了什么将设备“水平”旋转LOL我喜欢这篇文章。别难过,我们都会时不时地做这种事time@codeMagic绝对神奇,做一个答案,这样我就能把它标记正确