make a RTL navigation Drawer in android

2019-06-06 09:44发布

问题:

I want to make my navigation open from right to left. But as soon as I change any of these steps my program face to error force closed after click on navigation after running the app.

My main_activity.xml

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

My custom_toolbar.xml

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_gravity="right"
    android:background="@color/colorPrimary">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="title"
        android:padding="8dp"
        android:textColor="#fff"
        android:textSize="18sp"
        />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/cnews"
        android:tint="#fff"
        android:layout_gravity="right"
        />
</android.support.v7.widget.Toolbar>


//content
<include layout="@layout/content_main" /> </LinearLayout>

My appbar_main.xml

<include layout="@layout/toolbar_main" />

My main_activity.java

import android.app.FragmentManager; 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.ActionBarDrawerToggle;   import android.support.v7.app.AppCompatActivity;   import android.support.v7.widget.Toolbar; import android.view.Menu;   import android.view.MenuItem;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.home_page) {
            Home_page_fragment homeFragment = new Home_page_fragment();
            FragmentManager manager = getFragmentManager();
            manager.beginTransaction().replace(R.id.mainContent , homeFragment, homeFragment.getTag()).commit();
        } else if (id == R.id.tour_page) {
            Tour_page_fragment tourFragment = new Tour_page_fragment();
            FragmentManager manager = getFragmentManager();
            manager.beginTransaction().replace(R.id.mainContent , tourFragment, tourFragment.getTag()).commit();

        } else if (id == R.id.lstour_page) {
            LsTour_page_fragment LstourFragment = new LsTour_page_fragment();
            FragmentManager manager = getFragmentManager();
            manager.beginTransaction().replace(R.id.mainContent , LstourFragment, LstourFragment.getTag()).commit();

        } else if (id == R.id.hotel_page) {

        } else if (id == R.id.agency_page) {

        } else if (id == R.id.news_page) {

        }else if (id == R.id.logbook_page) {

        }else if (id == R.id.message_page) {

        }else if (id == R.id.favorite_page) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    } }

I changed "Start" to "end" in .xml main activity and java main activity get force closed!

I change "start" to "right" get force closed!!

I used RTL library but again get forced closed!!!

Thanks in advance.

回答1:

The problem is that your customized Gravity is not defined and returns NULL. First of all, you should update your menu.xml and build an item as an icon for right menu, like this:

<item
        android:id="@+id/menu_right"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:icon="@android:drawable/ic_dialog_dialer"
        android:title="@string/action_menuright"
/>

And in main_activity.java disable the default toggle drawer which opened from left and update this:

toggle.syncState();
toggle.setDrawerIndicatorEnabled(false);//this is added line

and similary udate this one:

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
         <   here:  add this line:  >
    DrawerLayout drawer;

and finally change this class to:

public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    // noinspection SimplifiableIfStatement
    if (id == R.id.menu_right) {
        if(drawer.isDrawerOpen(Gravity.RIGHT) ) {
             drawer.closeDrawer(Gravity.RIGHT);
        }
        else{
             drawer.openDrawer(Gravity.RIGHT);
        }
        return true;
    }

    return super.onOptionsItemSelected(item);
}

and everything is fine now.