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

Android布局问题:如何让Fragment占据LinearLayout与TableLayout间剩余空间

解决Fragment占据剩余空间的问题

你的核心问题是ConstraintLayout中未正确设置Fragment的底部约束,且错误使用了LinearLayout专属的layout_weight属性(ConstraintLayout不支持该属性,相关设置完全无效)。按以下修改即可实现需求:

  • 移除所有控件上无效的android:layout_weight属性(仅保留内部LinearLayout/TableRow里的layout_weight,因为它们属于LinearLayout子类,该属性有效)
  • 给中间Fragment添加app:layout_constraintBottom_toTopOf="@+id/tableLayout",将其底部与TableLayout顶部绑定
  • 给底部TableLayout添加app:layout_constraintBottom_toBottomOf="parent",确保它固定在页面底部,高度随内容自适应
  • Fragment高度保持0dp(即match_constraint),它会自动填充顶部LinearLayout和底部TableLayout之间的所有剩余空间

修改后的完整布局代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main">

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/textView10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Status:"
            android:textAppearance="@style/TextAppearance.AppCompat.Body2" />

        <TextView
            android:id="@+id/textViewStatus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:text="TextView" />

        <Switch
            android:id="@+id/switch1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Switch" />

    </LinearLayout>

    <fragment
        android:id="@+id/nav_host_fragment_content_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout2"
        app:layout_constraintBottom_toTopOf="@+id/tableLayout"
        app:navGraph="@navigation/mobile_navigation" />

    <TableLayout
        android:id="@+id/tableLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/nav_host_fragment_content_main"
        app:layout_constraintBottom_toBottomOf="parent">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textView16"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />

            <SeekBar
                android:id="@+id/seekBar5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textView17"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />

            <SeekBar
                android:id="@+id/seekBar6"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textView15"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView" />

            <SeekBar
                android:id="@+id/seekBar4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" />
        </TableRow>
    </TableLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

内容的提问来源于stack exchange,提问作者Lithium

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 19:11:01