【问题标题】:match_parent doesn't work with CoordinatorLayoutmatch_parent 不适用于 CoordinatorLayout
【发布时间】:2016-07-02 18:51:50
【问题描述】:

我使用 Android 设计支持库 (com.android.support:appcompat-v7:22.2.0),我在 CoordinatorLayout 中有一个 LinearLayout,如下所示:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <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/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <!-- Content -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#44ff44"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Yo Yo"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Yo Yo"
            />

    </LinearLayout>

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

<android.support.design.widget.NavigationView
    android:id="@+id/navigation"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header"
    app:itemIconTint="@color/nav_item_icon_tint_color"
    app:itemTextColor="@color/nav_item_text_color"
    app:menu="@menu/navigation_drawer_items" />

在代码中,我更改了背景颜色,并且我希望 BG 应该在显示屏上全部填充(因为我在 layout_width 和 layout_height 中使用了 match_parent),但我得到了这样的 BG 颜色:

看起来它使用wrap_content 比使用match_parent 显示更多。 我尝试在 LinearLayout 和 layout_centerInParent 中使用layout_weight,但它也不起作用。

我做错了什么?还是 API 错误?

更新:**

为了完成背景,我可以使用android:layout_gravity="fill_vertical"(@Abdullah 建议)。但我对这个问题的另一个观点是,我希望 LinearLayout 中的 match_parentlayout_weightlayout_centerInParent 以及 RelativeLayout 中的其他相对值在 CoordinatorLayout 下正常工作。现在,当我将片段添加到 LinearLayout 时,布局对齐(在片段布局中)与我的预期不同。使用比率或相对值的一切都行不通。

谢谢

【问题讨论】:

  • 尝试将 android:layout_gravity="fill_vertical" 添加到 LinearLayout。
  • @Abdullah 谢谢你的回答。您建议它可以完全填充背景颜色,但 layout_weight(在 LinearLayout 中)和 layout_centerInParent(在相对布局中)仍然无法正常工作。
  • 您需要在添加片段的地方使用“FrameLayout”。不需要在 Fragment 布局中更新。
  • @Watcharin.s 我可以知道你是如何解决这个问题的吗?

标签: android android-layout androiddesignsupport


【解决方案1】:

appbar_scrolling_view_behavior 的行为应该应用于实现ScrollingViewView

如果您在滚动主要内容时不需要自动隐藏 ToolbarLayout 或使用 RecyclerView 而不是 LinearLayout,请删除该行为。

【讨论】:

  • 感谢您的帮助,但我不确定您是否理解我的观点。我的观点是我希望 LinearLayout 中的 match_parentlayout_weight layout_centerInParent 以及 RelativeLayout 中的其他相对值在 CoordinatorLayout 下正常工作。现在,当我将片段添加到 LinearLayout 时,布局对齐方式与我的预期不同。所有与比率或相对值相关的东西都不起作用。
  • 如果上面的布局是主 Activity 的布局,工具栏作为“操作栏”,而不是 LinearLayout,内容视图是 FrameLayout,该怎么办?然后将 FrameLayout 用作片段的容器视图。如果 Fragment 包含像 RecyclerView 这样的可滚动视图,则工具栏应滚动,否则应保持固定。这基本上是我的设置,但我也面临一个问题,即尽管声明了 match_parent,但片段并没有填满整个空间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-22
  • 2018-08-27
  • 1970-01-01
  • 1970-01-01
  • 2015-10-11
相关资源
最近更新 更多