0

I'd like to create a navigation drawer effect, but rather than the drawer sliding out from the right.I Created code and my code working perfect when my slide menu is left side.this is a my java code.

DrawerLayout drawerLayout;
View drawerView;
View drawerContent;
View mainContent;
ActionBarDrawerToggle toggle;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerView = findViewById(R.id.drawer_view);
    drawerContent = findViewById(R.id.drawer_content);
    mainContent = findViewById(R.id.main_content);
    toggle = new ActionBarDrawerToggle(
            TestLayout.this,
            drawerLayout,
            R.drawable.ic_drawer,
            R.string.u_drawer_call_center,
            R.string.u_drawer_call_center
    ) {

        @Override
        public void onDrawerSlide(View drawer, float slideOffset)
        {
            super.onDrawerSlide(drawer, slideOffset);

            drawerContent.setX(drawerView.getWidth() * (1 - slideOffset));
            mainContent.setX(drawerView.getWidth() * slideOffset);

        }
        public void onDrawerClosed(View view) {
        }

        public void onDrawerOpened(View drawerView) {

        }
    };
    drawerLayout.setDrawerListener(toggle);
    drawerLayout.closeDrawer(drawerView);
    drawerContent.setX(drawerView.getWidth());
}

} as i said i can move main content when my slide menu is left side.but now i want slide menu right side.i rewrited my xml code but i can't move main content this is a my xml code

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">

<FrameLayout android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#cccccc" />

<LinearLayout android:id="@+id/drawer_view"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="right">

    <LinearLayout android:id="@+id/drawer_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/ic_launcher" />

    </LinearLayout>

</LinearLayout>

i also changed source code

drawerContent.setX(drawerLayout.getWidth() - (drawerView.getWidth() * (1 - slideOffset)));
mainContent.setX(drawerLayout.getWidth() - (drawerView.getWidth() *   slideOffset));

but this code not working.I don't know how to use my code when i have slide menu right side. if anyone knows solution please help me thanks everyone

4

1 回答 1

0

除了像您已经完成的那样更改layout_gravityon the之外,必要的更改很小。drawer_view我的原始代码中,否定每次出现的drawerView.getWidth(). 那是:

toggle = new ActionBarDrawerToggle(...) {
    ...

    @Override
    public void onDrawerSlide(View drawer, float slideOffset) {
        super.onDrawerSlide(drawer, slideOffset);

        drawerContent.setX(-drawerView.getWidth() * (1 - slideOffset));
        mainContent.setX(-drawerView.getWidth() * slideOffset);

        ...
    }
};
...

drawerContent.setX(-drawerView.getWidth());

然后,您将需要覆盖MainActivityonOptionsItemSelected()方法,如下所示:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        if(drawerLayout.isDrawerOpen(Gravity.END)) {
            drawerLayout.closeDrawer(Gravity.END);
        }
        else {
            drawerLayout.openDrawer(Gravity.END);
        }
        return true;
    }
    ...

    return super.onOptionsItemSelected(item);
}
于 2015-12-03T22:01:03.667 回答