【发布时间】: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_parent、layout_weight 或 layout_centerInParent 以及 RelativeLayout 中的其他相对值在 CoordinatorLayout 下正常工作。现在,当我将片段添加到 LinearLayout 时,布局对齐(在片段布局中)与我的预期不同。使用比率或相对值的一切都行不通。
谢谢
【问题讨论】:
-
尝试将 android:layout_gravity="fill_vertical" 添加到 LinearLayout。
-
@Abdullah 谢谢你的回答。您建议它可以完全填充背景颜色,但
layout_weight(在 LinearLayout 中)和layout_centerInParent(在相对布局中)仍然无法正常工作。 -
您需要在添加片段的地方使用“FrameLayout”。不需要在 Fragment 布局中更新。
-
@Watcharin.s 我可以知道你是如何解决这个问题的吗?
标签: android android-layout androiddesignsupport