对象下的导航抽屉

时间:2014-12-29 17:19:30

标签: android navigation-drawer material-design

我已在我的应用程序中实现了导航样式抽屉材料设计,但是一旦打开它就会显示在所有对象下面。

dwr dwr2

我的代码

layout.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <!-- Action-bar looking view -->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/actionbar_dimen"
        android:background="@color/primary_green"
        android:id="@+id/frameLayout">

        <ImageView
            android:id="@+id/drawer_indicator"
            android:layout_width="@dimen/actionbar_dimen"
            android:layout_height="56dp"
            android:scaleType="centerInside"
            android:background="@drawable/drawer_selector"
            android:layout_gravity="start"
            />

        <TextView
            android:id="@+id/indicator_style"
            android:layout_width="wrap_content"
            android:layout_height="56dp"
            android:paddingLeft="12dp"
            android:paddingRight="12dp"
            android:layout_gravity="end"
            android:text="@string/rounded"
            android:textStyle="bold"
            android:textColor="@color/primary_dark_green"
            android:gravity="center"
            android:background="@drawable/drawer_selector"
            />

    </FrameLayout>

    <!-- Content -->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/actionbar_dimen"
        android:background="@color/primary_green"
        android:layout_below="@+id/frameLayout"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/frameLayout2"/>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_below="@+id/frameLayout2"
        android:layout_alignParentBottom="true"
        android:background="@color/fondo">

        <TextView
            android:id="@+id/view_content"
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:gravity="center"
            android:text="@string/content_hint"
            />

        <TextView
            android:id="@+id/drawer_content"
            android:layout_width="300dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:gravity="center"
            android:text="@string/drawer_hint"
            android:textColor="@color/primary_dark_green"
            android:background="@color/light_green"
            />

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

    <RelativeLayout
        android:layout_width="1100dp"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_alignTop="@+id/frameLayout2"
        android:background="@drawable/rounded_corner">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="@dimen/actionbar_dimen"
            android:background="@color/blanco"
            android:id="@+id/titulo_interno"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true">
        </RelativeLayout>
    </RelativeLayout>

</RelativeLayout>

class.java

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import static android.view.Gravity.START;

public class RegistrarInciTraActivity extends ActionBarActivity
{

    private DrawerArrowDrawable drawerArrowDrawable;
    private float offset;
    private boolean flipped;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registrar_inci_traf_layout);

        final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        final ImageView imageView = (ImageView) findViewById(R.id.drawer_indicator);
        final Resources resources = getResources();

        drawerArrowDrawable = new DrawerArrowDrawable(resources);
        drawerArrowDrawable.setStrokeColor(resources.getColor(R.color.light_green));
        imageView.setImageDrawable(drawerArrowDrawable);

        drawer.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {
            @Override public void onDrawerSlide(View drawerView, float slideOffset) {
                offset = slideOffset;

                // Sometimes slideOffset ends up so close to but not quite 1 or 0.
                if (slideOffset >= .995) {
                    flipped = true;
                    drawerArrowDrawable.setFlip(flipped);
                } else if (slideOffset <= .005) {
                    flipped = false;
                    drawerArrowDrawable.setFlip(flipped);
                }

                drawerArrowDrawable.setParameter(offset);
            }
        });

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                if (drawer.isDrawerVisible(START)) {
                    drawer.closeDrawer(START);
                } else {
                    drawer.openDrawer(START);
                }
            }
        });

        final TextView styleButton = (TextView) findViewById(R.id.indicator_style);
        styleButton.setOnClickListener(new View.OnClickListener() {
            boolean rounded = false;

            @Override public void onClick(View v) {
                styleButton.setText(rounded //
                        ? resources.getString(R.string.rounded) //
                        : resources.getString(R.string.squared));

                rounded = !rounded;

                drawerArrowDrawable = new DrawerArrowDrawable(resources, rounded);
                drawerArrowDrawable.setParameter(offset);
                drawerArrowDrawable.setFlip(flipped);
                drawerArrowDrawable.setStrokeColor(resources.getColor(R.color.light_green));

                imageView.setImageDrawable(drawerArrowDrawable);
            }
        });
    }
}

我尝试移动相对布局,但它永远不会工作,或许有一些方法可以放在对象上方?我怎么解决这个问题?

由于

(如果您需要更多代码,请告诉我并编辑此问题)

1 个答案:

答案 0 :(得分:2)

来自here

  

在DrawerLayout内,添加一个包含屏幕主要内容的视图(隐藏抽屉时的主要布局)和另一个包含导航抽屉内容的视图。

所以你应该:

<android.support.v4.widget.DrawerLayout>

    <LinearLayout>

        < /> <!-- action bar code here -->
        <RelativeLayout /> <!-- main layout here -->

    </LinearLayout>

    <RelativeLayout>  <!-- drawer stuff here -->
        android:layout_gravity="start"
    </RelativeLayout>

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

这种方式 - 我认为 - 抽屉将覆盖你的假动作栏。为避免这种情况,请将android:layout_marginTop="@dimen/actionbar_dimen"设置为“抽屉内容”RelativeLayout(其高度设置为match_parent)。无论如何,根据材料设计指南,覆盖ActionBar实际上是导航抽屉的推荐行为。

这意味着要告别汉堡到箭头的动画,我知道。

我补充一点,你可能需要重新调整你的布局高度(摆脱重量),也许你的代码。但请继续使用<android.support.v4.widget.DrawerLayout> ID指向抽屉。

如果您使用的是Android Studio,则可以轻松导入“Nav Drawer”示例,以查看一些正常工作的代码和布局。不了解Eclipse。