片段没有填满整个屏幕/父布局Android

时间:2018-01-29 01:14:50

标签: android android-layout android-fragments android-coordinatorlayout android-framelayout

我的应用程序中有一个FrameLayout正在被包含RecyclerView的Fragment替换,但是当它这样做时,RecyclerView不会填满整个FrameLayout,它周围有一个边框。

Image

父布局,这是从MainActivity调用的,它包含将被替换的FrameLayout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="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:fitsSystemWindows="true"
android:orientation="vertical"
tools:context="com.jggdevelopment.wannacook.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>

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


<FrameLayout
    android:id="@+id/relative_layout_for_fragment"
    tools:showIn="@layout/navigation_toolbar"
    tools:context="com.jggdevelopment.wannacook.MainActivity"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

</FrameLayout>

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

将其替换为RecyclerView:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/fridge_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"/>

    <com.getbase.floatingactionbutton.FloatingActionsMenu
        android:id="@+id/fab_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/floating_button"
        android:layout_alignParentEnd="true"
        android:layout_gravity="bottom|end"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        fab:fab_labelStyle="@style/menu_labels_style"
        fab:fab_labelsPosition="left"
        fab:fab_icon="@drawable/ic_plus_white_24dp"
        fab:fab_addButtonColorNormal="@color/colorAccent"
        fab:fab_addButtonColorPressed="@color/colorAccentPressed">

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="addNewItem"
            fab:fab_colorNormal="@color/white"
            fab:fab_colorPressed="@color/whitePressed"
            fab:fab_title="Add new item"
            fab:fab_icon="@drawable/ic_restaurant_black_24dp"
            />

    </com.getbase.floatingactionbutton.FloatingActionsMenu>

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

MainActivity中用于替换Fragment的代码:

else if (id == R.id.nav_fridge) {
        FridgeFragment fridgeFragment = new FridgeFragment();
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(
                R.id.relative_layout_for_fragment,
                fridgeFragment,
                fridgeFragment.getTag()
        ).commit();
    }

2 个答案:

答案 0 :(得分:1)

请删除FrameLayout上的填充。

android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"

答案 1 :(得分:1)

  

在帧布局中进行此更改

     

删除:

android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
  

或者在布局中使用此框架布局(我已做出更改)

 <FrameLayout
            android:id="@+id/relative_layout_for_fragment"
            tools:showIn="@layout/navigation_toolbar"
            tools:context="com.jggdevelopment.wannacook.MainActivity"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:android="http://schemas.android.com/apk/res/android">

        </FrameLayout>