Fragment所在的layout不显示button的问题

这是一个Fragment的Layout,直接在里面添加了一个button控件,但是页面里不会展示出这个button控件,运行到模拟器上也不会显示

问题:是button控件放置的位置有错误么?还是哪里没有绑定么?

id="@+id/first_fragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:textAlignment="center"
    android:textSize="20sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:text="这是第一个Fragment" />

img

img

你的xml布局的根视图组件用的是LinearLayout,而且你还让它的方向设置为了horizontal,横向排列。
另外你的TextView的宽度是match_parent,导致button可用空间被挤压没了。

方式一:你可以设置LinearLayout的orientation = "vertical",就能看到button了。

方式二:你使用layout_weight来调整TextView和button的占比

<LinearLayout android:layout_width="match_parent" 
     android:layout_height="wrap_content"
     android:orientation="horizontal">
>

    <TextView android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="超长文本内容"/>

    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="我是按钮"/>

</LinearLayout>

如果帮助到你解决了问题,请采纳,谢谢。