You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android新手求助:LinearLayout底部按钮无法显示问题(Udacity旧课程学习遇障碍)

解决LinearLayout底部Button无法显示的问题

嘿,刚看了你的布局代码,问题一下子就找到了——你中间的quantity_text_view设置了android:layout_height="match_parent",这会让它占满垂直方向上所有剩余的空间,直接把底部的Button给挤到屏幕外面去了,所以你才看不到它。

给你两个简单的解决办法,按需选择就行:

方法1:让TextView仅包裹内容

如果你不需要这个数字TextView占满剩余空间,只是用来显示数量,直接把它的layout_height改成wrap_content即可:

<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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Quantity" />

    <TextView
        android:id="@+id/quantity_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" <!-- 这里修改了高度属性 -->
        android:text="0"
        android:textColor="@color/black"
        android:textSize="16sp" />

    <Button
        android:id="@+id/button_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Order" />
</LinearLayout>

方法2:让TextView占满中间空间,Button固定在底部

如果你希望数字TextView能填充顶部标题和底部Button之间的所有空间,就用layout_weight属性来分配空间:把TextView的layout_height设为0dp,再加上android:layout_weight="1",这样它会自动占据除了顶部标题和底部Button之外的全部垂直空间:

<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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Quantity" />

    <TextView
        android:id="@+id/quantity_text_view"
        android:layout_width="match_parent"
        android:layout_height="0dp" <!-- 修改高度为0dp -->
        android:layout_weight="1" <!-- 新增权重属性分配剩余空间 -->
        android:text="0"
        android:textColor="@color/black"
        android:textSize="16sp" />

    <Button
        android:id="@+id/button_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Order" />
</LinearLayout>

老课程确实容易踩这类小坑,毕竟Android布局的细节规范这些年有不少优化,遇到布局显示问题时,先检查各个控件的宽高设置,大概率能找到原因~

内容的提问来源于stack exchange,提问作者Wasim Akram Biswas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 08:52:44