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

Android Studio中ConstraintLayout设置match_parent不生效问题

解决Fragment中ConstraintLayout未占满父容器且出现滚动的问题

嘿,我来帮你搞定这个布局问题!看了你的代码,发现几个关键问题导致ConstraintLayout没占满父容器还出现了滚动:

1. Activity中Fragment容器的配置问题

你的Activity用了CoordinatorLayout和AppBarLayout,但如果Fragment的容器(比如FrameLayout)没正确配置,会被AppBarLayout挤压,导致布局高度不足还出现滚动空间。要做这两点:

  • 给Fragment容器设置android:layout_height="match_parent"
  • 添加app:layout_behavior="@string/appbar_scrolling_view_behavior",让容器和AppBarLayout联动,自动避开AppBar的高度

2. Fragment中ListView的固定高度坑

你给ListView设了android:layout_height="300sp"固定高度,这会让它没法自适应父容器的剩余空间,而且和底部的FloatingActionButton约束后,容易导致整体布局高度超过父容器,出现滚动。应该改成:

  • android:layout_height="0dp"(也就是match_constraint)
  • 约束它的顶部到et_avversario的底部,底部到FloatingActionButton的顶部,让它自动填充中间的剩余空间

3. Fragment中错误的约束设置

你的et_avversario设置了app:layout_constraintEnd_toStartOf="@+id/ListView01"和app:layout_constraintStart_toEndOf="@+id/ListView01",这相当于把它挤到ListView的外面,布局计算会出错,间接导致整体布局异常。应该改成约束到父容器的左右两侧,顶部接et_luogo的底部。


修正后的Fragment布局代码

<android.support.constraint.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"
    android:background="@android:color/holo_blue_light"
    tools:context="com.saffru.colombo.soccerstats.match.PrePartitaFragment">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Prepartita"
        android:textColor="@android:color/black"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/txtdate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:focusable="false"
        android:hint="Tocca per inserire la data*"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:text="In casa"
        app:layout_constraintTop_toBottomOf="@+id/txtdate" />

    <EditText
        android:id="@+id/et_luogo"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:ems="10"
        android:hint="Luogo"
        android:inputType="textPersonName|textCapSentences"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/checkBox"
        app:layout_constraintTop_toBottomOf="@+id/txtdate" />

    <EditText
        android:id="@+id/et_avversario"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:ems="10"
        android:hint="Avversario"
        android:inputType="textPersonName|textCapSentences"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/et_luogo" />

    <ListView
        android:id="@+id/ListView01"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        app:layout_constraintBottom_toTopOf="@+id/submit"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/et_avversario" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:background="@color/colorAccent"
        android:src="@drawable/ic_check"
        android:textColor="#fff"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</android.support.constraint.ConstraintLayout>

修正后的Activity布局代码(补全并优化)

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
    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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".match.PartitaSpinnerActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <!-- Fragment容器:设置match_parent高度+联动AppBar的behavior -->
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

最后再检查这几点:

  • 确保你的Fragment是正确添加到fragment_container这个FrameLayout里的
  • 如果ListView内容过多,它自己会内部滚动,不会导致整个页面滚动
  • 所有元素的约束都要明确,避免出现“游离”的控件导致布局计算异常

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 10:17:24