负边距布局以覆盖父布局

时间:2017-11-22 11:02:12

标签: java android android-fragments

我有一个活动,它有一个工具栏和一个framelayout,我可以在其中注入片段。

这是该活动的布局:

<android.support.v4.widget.DrawerLayout
        android:id="@+id/drwDrawerRoot"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background_main_selector"
        android:theme="@style/AppTheme">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <include
                android:id="@+id/viewMainToolbar"
                layout="@layout/view_toolbar" />

        </LinearLayout>

        <FrameLayout
            android:id="@+id/frmDrawerContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/toolbar_height"
            android:clipChildren="false"
            android:clipToPadding="false"
            android:orientation="vertical" />

        <include
            android:id="@+id/viewDrawer"
            layout="@layout/view_drawer"
            bind:name="@{name}"/>

        <include
            android:id="@+id/viewUserDrawer"
            layout="@layout/view_user_drawer"
            bind:name="@{name}"/>

    </android.support.v4.widget.DrawerLayout>

我有一个带边距的framelayout,以便片段中的内容不会覆盖工具栏,但是我将clipChildren和clipToPadding设置为false,就像我在这里看到的其他帖子一样。

在某些片段上,我有一个加载视图,我想占用整个屏幕。

这是一个示例片段:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/layout_main"
            style="@style/Layout.FullScreen"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/layout_footer"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView7"
                style="@style/Text.Header"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/login_header" />

            <EditText
                android:id="@+id/edtMessage"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:elevation="1dp"
                android:ems="10"
                android:inputType="textMultiLine" />

            <Button
                android:id="@+id/btnSend"
                style="@style/Button.Primary"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/support_button" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/layout_footer"
            style="@style/Layout.FullScreen.Footer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="vertical">

            <TextView
                android:id="@+id/footerInfo"
                style="@style/Text.White.Small.Centered"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/footer_info" />
        </LinearLayout>

        <include
            layout="@layout/view_loading"
            android:visibility="invisible"
            bind:loading="@{loading}" />

    </RelativeLayout>

这是包含的加载视图,我设置了负边距,希望它会移动到屏幕顶部:

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="-40dp"
        android:background="@color/colorLoadingBackground"
        android:clickable="true"
        android:visibility="@{loading ? View.VISIBLE : View.GONE}">

    </FrameLayout>

我在这里错过了什么吗?

这是可行的还是我需要改变我这样做的方式?

澄清: 第一个/根布局包括工具栏,第二个布局包含在第一个布局中。这意味着第二个布局的内容从工具栏下方开始。但是,我希望第三个布局(包含在第二个布局中的加载屏幕)具有负边距,以便它覆盖整个屏幕,而不仅仅是从工具栏下方开始。

1 个答案:

答案 0 :(得分:0)

您需要使用CoordinatorLayout,然后您不需要负边距。这里是你可以根据需要定制的样本布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.CoordinatorLayout
        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:theme="@style/AppTheme.AppBarOverlay"
            app:layout_behavior= "@string/appbar_scrolling_view_behavior">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:title="My App" />
        </android.support.design.widget.AppBarLayout>

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

        <!--include a full screen loading layout here and hide/show according to your need-->
        <include layout="@layout/loading_layout/>

    </android.support.design.widget.CoordinatorLayout>
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"/>
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>