导航抽屉的Android拖动边距在屏幕外

时间:2015-08-07 13:07:34

标签: android android-studio navigation-drawer

我最近创建了一个名为Hoops的应用程序,我已经开发了很长一段时间了。然而,我似乎得到的主要问题是,导航抽屉的滑动边距在屏幕外,而其他用户发现使用导航抽屉的滑动功能非常困难。菜单按钮工作得很好。我已经在线查看了其他教程,所有这些教程似乎都显示了与我所链接的论坛中的答案相同的答案:Set drag margin for Android Navigation Drawer 但是,我尝试过这个解决方案,它似乎对我不起作用。如果有人有任何想法,为什么它不工作,请你帮助我。以下是使用导航抽屉的主要活动的编码:

package com.jehan.sportstutorials;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

DrawerLayout drawerLayout;
Toolbar toolbar;
ActionBar actionBar;
TextView textView;

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

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    actionBar = getSupportActionBar();
    actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
    actionBar.setDisplayHomeAsUpEnabled(true);


    drawerLayout = (DrawerLayout) findViewById(R.id.navigation_drawer_layout);

    NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
    if (navigationView != null) {
        setupNavigationDrawerContent(navigationView);
    }

    setupNavigationDrawerContent(navigationView);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            drawerLayout.openDrawer(GravityCompat.START);
            return true;
    }
    return super.onOptionsItemSelected(item);
}

private void setupNavigationDrawerContent(NavigationView navigationView) {
    navigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    textView = (TextView) findViewById(R.id.textView);
                    switch (menuItem.getItemId()) {
                        case R.id.item_navigation_drawer_basketball:
                            menuItem.setChecked(true);
                            textView.setText(menuItem.getTitle());
                            drawerLayout.closeDrawer(GravityCompat.START);
                            Intent i = new Intent(MainActivity.this, BasketballTutorial.class);
                            startActivity(i);
                            return true;
                        case R.id.item_navigation_drawer_help:
                            menuItem.setChecked(true);
                            textView.setText(menuItem.getTitle());
                            drawerLayout.closeDrawer(GravityCompat.START);
                            Intent intent2 = new Intent(MainActivity.this, HelpScreen.class);
                            startActivity(intent2);
                            return true;
                    }
                    return true;
                }
            });
}

}

activity_main.xml中

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


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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/status_bar_kitkat_height"
        android:background="?colorPrimary"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="?colorPrimaryDark"/>

</LinearLayout>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/welcome"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        android:textColor="@color/md_text"
        android:singleLine="false"
        android:padding="20dp"
        android:gravity="center"
        android:textStyle="bold"
        android:textIsSelectable="false" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|bottom"
        android:text="@string/swipe"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        android:textColor="@color/md_text"
        android:singleLine="false"
        android:padding="20dp"
        android:gravity="center"
        android:textSize="20sp"
        android:textStyle="italic"
        android:layout_marginBottom="30dp"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:background="?colorPrimaryDark"/>

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:minHeight="?attr/actionBarSize"
        android:background="@drawable/action_bar_color"
        android:elevation="4dp"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ToolbarTheme"
        android:layout_marginTop="25dp"/>

</FrameLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="@bool/fitsSystemWindows"
    app:headerLayout="@layout/navigation_drawer_header"
    app:menu="@menu/navigation_drawer_menu"
    app:theme="@style/NavigationViewTheme" />

如果有人想要导航抽屉滑动功能不起作用的实例,您可以在应用商店中测试我的应用。这是链接:https://play.google.com/store/apps/details?id=com.jehan.sportstutorials

总结:有没有人知道如何增加导航抽屉的拖拽边距,但不遵循我的第一段链接中所述的传统方法?帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

检查R.layout.activity_main布局。 Android Studio IDE出于某种原因,将15dp边距设置为自动创建的Activity布局的最顶层容器。这可能会干扰Drawer

只需检查最顶层的相对/框架/无论哪个布局都设置了android:layout_marginX属性并将其删除。