NavigationView 标头中的 ConstraintLayout 不采用全宽

发布于 2025-01-12 20:53:08 字数 1195 浏览 6 评论 0原文

这是我的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">

这是我的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 不起作用。标题宽度小于 NavigationView

当我尝试将 ConstraintLayout 替换为 LinearLayout 时,width 是正确的。

有人可以帮助我理解为什么 ConstraintLayout 在这种情况下不正确吗?

Here is my 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:

<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. Header width is less then NavigationView.

When I'm trying to replace ConstraintLayout to LinearLayout, width is correct.

Can somebody help me understand why does ConstraintLayout not fit correctly in this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何必那么矫情 2025-01-19 20:53:08

好吧,我完全不知道,但你可以尝试一下,将宽度更改为 0dp 并在 xml 中添加此行并尝试

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

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

android:layout_width="0dp"
app:layout_constraintWidth_percent="1"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文