简体   繁体   English

负边距布局以覆盖父布局

[英]Negative margin Layout to overlay parent layout

I have an activity that has a toolbar and a framelayout where I inject fragments. 我有一个活动,该活动具有工具栏和框架布局,可以在其中插入片段。

This is the layout for that activity: 这是该活动的布局:

<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>

I have the framelayout with a margin so that the contents from the fragments do not overlay the toolbar, but I set clipChildren and clipToPadding to false as per some other posts I have seen here. 我在framelayout上留有一个边距,以使片段中的内容不会覆盖工具栏,但是根据我在这里看到的其他一些文章,我将clipChildren和clipToPadding设置为false。

On some fragments however I have a loading view, which I would like to occupy all the screen. 但是在某些片段上,我有一个加载视图,我想占据所有屏幕。

This is a sample fragment: 这是一个示例片段:

<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>

Ans this is the included loading view, which I have set with negative margin, hoping it would move up to the top of the screen: 回答:这是包含的加载视图,我已将其设置为负边距,希望它会向上移动到屏幕顶部:

<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>

Am I missing something here? 我在这里想念什么吗?

Is this doable or do I need to change how I am doing this? 这是可行的还是我需要更改我的操作方式?

Clarification: The first / root layout includes a toolbar and the second layout is included within the frame layout of the first. 说明:第一个/根目录布局包含一个工具栏,第二个布局包含在第一个/根目录的框架布局中。 this means that the content of the second layout starts below the toolbar. 这意味着第二个布局的内容从工具栏下方开始。 However, I would like the third layout (a loading screen that is included in the second layout) to have a negative margin so that it overlays the full screen, not just starting below the toolbar. 但是,我希望第三个布局(第二个布局中包含的加载屏幕)具有负边距,以便它覆盖整个屏幕,而不仅仅是从工具栏下方开始。

You need to use CoordinatorLayout then you wont need negative margins. 您需要使用CoordinatorLayout然后就不需要负边距。 here are the sample layout u can customise according to your need 这是您可以根据需要自定义的示例布局

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM