匹配父级不占用布局的整个宽度
Match parent not occupying whole width for layout
我有点困惑为什么我的布局没有占据整个屏幕宽度。它使用 match_parent 作为父根容器,预览显示整个屏幕都被占用了,但是当我 运行 它时,我看到了这个,其中显示了一大块 space。
我什至尝试使用 fitsSystemWindows 和 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>
此布局在主机内部 activity。
<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>
谢谢。
找到答案了。 activity_main 文件中的 NestedScrollView 中缺少 android:fillViewport="true"
。
我有点困惑为什么我的布局没有占据整个屏幕宽度。它使用 match_parent 作为父根容器,预览显示整个屏幕都被占用了,但是当我 运行 它时,我看到了这个,其中显示了一大块 space。
我什至尝试使用 fitsSystemWindows 和 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>
此布局在主机内部 activity。
<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>
谢谢。
找到答案了。 activity_main 文件中的 NestedScrollView 中缺少 android:fillViewport="true"
。