android - 按钮未显示在布局中

标签 android xml android-layout

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

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/

相关文章:

android - 如何使用 new places sdk for android 查找附近的地方?

Android 6.0 定位权限

android - 如何从android中的firebase数据库中检索特定节点

java - 如何使用命名空间针对 xsd 验证 xml 文件

android - 如何在 Android Studio 中垂直居中 ConstraintLayout 内容?

java - 从字符串而不是文件解析 XML 时出现问题

xml - 删除 XML 标记及其内容之间的任何内容

java - 在 recyclerview 或 listview 中 float 部分栏,如电报

android - 旋转后找不到 fragment 的 id 0x View ?

xml - 访问多个源的同名元素