乃是怪人 2023-01-10 11:23 采纳率: 80%
浏览 13
已结题

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

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

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

<TextView
    android: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" />

<Button
    android:id="@+id/btn_first"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:text="传递数据给second" />

img

img

  • 写回答

2条回答 默认 最新

  • Halifax ‎ 2023-01-10 11:32
    关注

    你的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>
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月18日
  • 已采纳回答 1月10日
  • 创建了问题 1月10日