简体   繁体   English

NavigationView header 中的 ConstraintLayout 不占满宽度

[英]ConstraintLayout in NavigationView header doesn't take full width

Here is my NavigationView :这是我的NavigationView

<com.google.android.material.navigation.NavigationView
    android:id="@+id/navView"
    android:layout_width="500dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header_test"
    android:theme="@style/NavigationViewStyleLand"
    app:itemTextColor="@color/mainGray"
    app:menu="@menu/navdrawer_menu">

Here is my HeaderLayout :这是我的HeaderLayout

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navHeader"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    android:background="@color/colorPrimary">
    ...
</androidx.constraintlayout.widget.ConstraintLayout>

android:layout_width="match_parent" for ConstraintLayout not working. android: ConstraintLayoutandroid:layout_width="match_parent"不工作。 Header width is less then NavigationView . Header 宽度小于NavigationView

When I'm trying to replace ConstraintLayout to LinearLayout , width is correct.当我尝试将ConstraintLayout替换为LinearLayout时, width是正确的。

Can somebody help me understand why does ConstraintLayout not fit correctly in this case?有人可以帮助我理解为什么ConstraintLayout在这种情况下不适合吗?

Well I exactly don't know but you can give it a try by changing the width to 0dp and add this line in your xml and try好吧,我完全不知道,但你可以通过将宽度更改为 0dp 来尝试一下,并将这一行添加到你的 xml 中并尝试

android:layout_width="0dp"
app:layout_constraintWidth_percent="1"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM