但是,当将按钮放置在层次结构中以便可以在导航抽屉的顶部看到它时,该按钮将正常工作。但是,将按钮滑出时,该按钮应该隐藏在导航抽屉的后面,因此这是不希望的。

下面是MainActivity.java中的代码

public class MainActivity extends ActionBarActivity implements NavigationDrawerCallbacks {

public ProgressDialog progBar;

public final static boolean DEBUG = false;
public final static String TAG = "AppGetter";
private Toolbar mToolbar;
private NavigationDrawerFragment mNavigationDrawerFragment;



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

    mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
    mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);

    ImageButton cart_button = (ImageButton) findViewById(R.id.button2);
    cart_button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            start_request();

        }
    });



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public void onNavigationDrawerItemSelected(int position) {
    Toast.makeText(this, "Menu item selected -> " + position, Toast.LENGTH_SHORT).show();
}

@Override
public void onBackPressed() {
    if (mNavigationDrawerFragment.isDrawerOpen())
        mNavigationDrawerFragment.closeDrawer();
    else
        super.onBackPressed();
}


public void start_request()
{
    String pkg = getPackageName();
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setComponent(new ComponentName(pkg,pkg+".RequestActivity"));
    startActivity(intent);

    if(DEBUG)Log.v(TAG,"Intent intent: "+intent);
}
}


我假设问题出在上面的类之内,但是为了完整起见,我在下面粘贴了两个感兴趣的XML文件。

activity_main.xml如您所见,ImageButton当前位于层次结构中的导航抽屉上方,以便使其被导航抽屉覆盖。将ImageButton移动到“下方”可使按钮正常工作,但会使其出现在导航抽屉的顶部(并且不会像其余布局一样着色)。

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:fontify="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:background="@color/myPrimaryColor">

    <View
        android:id="@+id/block1"
        android:layout_height="240dp"
        android:layout_width="match_parent"
        android:layout_below="@+id/toolbar_actionbar"
        android:background="@drawable/block_primary"
        />

    <TextView
        android:id="@+id/title1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_below="@+id/toolbar_actionbar"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="56dp"
        android:text="@string/title"
        android:textColor="@color/white"
        android:textSize="34sp"
        android:fontFamily="sans-serif"
        />

    <include
        android:id="@+id/toolbar_actionbar"
        layout="@layout/toolbar_default"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        />

    <ImageButton
        android:id="@+id/button2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="268dp"
        android:src="@drawable/ic_chevron_up"
        android:background="@drawable/fab_simple"/>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_below="@+id/toolbar_actionbar"
        >

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <fragment
            android:id="@+id/fragment_drawer"
            android:name="com.onepersonco.iconrequestbase.NavigationDrawerFragment"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            layout="@layout/fragment_navigation_drawer"
            tools:layout="@layout/fragment_navigation_drawer" />
    </android.support.v4.widget.DrawerLayout>
</RelativeLayout>




fragment_navigation_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<!-- Provides a margin at the top of the navigation drawer. -->
<View
    android:id="@+id/navWhiteSpace1"
    android:layout_width="match_parent"
    android:layout_height="64dp"
    android:background="@color/myNavigationDrawerBackgroundColor"
    />


<android.support.v7.widget.RecyclerView
android:id="@+id/drawerList"
android:layout_below="@+id/navWhiteSpace1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:scrollbars="vertical"
android:scrollbarDefaultDelayBeforeFade="0"
android:scrollbarFadeDuration="0"
android:overScrollMode="never"
android:focusable="true"
android:background="@color/myNavigationDrawerBackgroundColor"/>

</RelativeLayout>

最佳答案

尝试在线阅读各种导航抽屉教程时遇到了相同的问题。

对我有用的是在GitHub上使用Mike Penz的MaterialDrawer库。在页面底部的“自述文件”中,他有一个非常简单的教程。

希望其他对Java有更好了解的人可以解释您的代码为什么失败。

08-18 18:13