【问题标题】:Match parent not occupying whole width for layout匹配父级不占布局的整个宽度
【发布时间】:2024-09-26 14:10:01
【问题描述】:

我有点困惑,为什么我的布局没有占据整个屏幕宽度。它使用 match_parent 作为父根容器,预览显示整个屏幕被占用,但是当我运行它时,我看到的是这个,其中显示了一大块空间。

我什至尝试使用 fitSystemWindows 和 layout_weight 强制执行此操作,但到目前为止没有成功。

<?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"
    android:layout_weight="1"
    android:fitsSystemWindows="true"
    android:background="@color/shadow">

    <include
        android:id="@+id/titleAndProgressBar"
        layout="@layout/title_and_progress_bar" />

    <TextView
        android:id="@+id/tvThankYouLeave"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:text="Thank you for helping us fight COVID-19. We're sorry to see you go. When you tap 'I want to leave' we will remove all data stored by the app from your device including a mobile number, if you have shared one, symptom data and any demographic health data you may have shared. Non-identifying authorisation tokens stored on the server will also be deleted.\n\nRandom IDs created or collected by Exposure Notification Services cannot be removed by the COVID Tracker app. If you wish to remove these Random IDs you can do this via your device Settings."
        android:textColor="@color/text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/titleAndProgressBar" />

    <TextView
        android:id="@+id/tvReadPrivacy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:text="You can read the Data Protection \nInformation Notice here"
        android:textColor="@color/shadow"
        app:layout_constraintBottom_toBottomOf="@+id/ivLockPrivacy"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/ivLockPrivacy"
        app:layout_constraintTop_toTopOf="@+id/ivLockPrivacy" />

    <ImageView
        android:id="@+id/ivLockPrivacy"
        android:layout_width="42dp"
        android:layout_height="42dp"
        android:layout_marginTop="32dp"
        android:src="@drawable/privacy"
        app:layout_constraintBottom_toTopOf="@+id/btnLeave"
        app:layout_constraintStart_toStartOf="@+id/tvThankYouLeave"
        app:layout_constraintTop_toBottomOf="@+id/tvThankYouLeave"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/btnLeave"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:background="@drawable/rectangle_button"
        android:backgroundTint="@color/danger"
        android:text="I want to leave"
        android:textColor="@color/white"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tvReadPrivacy"
        app:layout_constraintVertical_bias="1.0" />     
 </androidx.constraintlayout.widget.ConstraintLayout>

这个布局在一个宿主活动中。

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/snippet_toolbar_plain" />

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:visibility="visible">

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:navGraph="@navigation/nav_graph" />

    </androidx.core.widget.NestedScrollView>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/white"
        app:itemIconSize="20dp"
        app:labelVisibilityMode="labeled"
        app:itemIconTint="@drawable/nav_items_color"
        app:itemTextColor="@drawable/nav_items_color"
        android:theme="@style/BottomNavigationStyle"
        app:menu="@menu/bottom_menu_nav" />

</LinearLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

谢谢。

【问题讨论】:

  • 这个 XML 文件是否加载到另一个视图中,比如片段或其他什么?
  • 1- 从父标签 ConstraintLayout 中删除 layout_weight。 2- 如果您将此 XML 文件作为其他布局文件的一部分加载,那么您应该在那里将高度更改为 match_parent。
  • 嗨,请参阅更新后的问题,其中包含此布局所在的主机活动的代码。
  • 如果你删除 app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" 会发生什么?
  • 您说的是片段 XML 文件?相同的空格。

标签: android android-layout android-constraintlayout


【解决方案1】:

找到了答案。在 activity_main 文件中的 NestedScrollView 中缺少 android:fillViewport="true"

【讨论】:

    最近更新 更多