我想从right to left
打开导航。但是一旦我改变了这些步骤,我的程序就会在运行应用程序后单击导航后关闭错误强制。
我的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" />
我的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>
我的appbar_main.xml
<include layout="@layout/toolbar_main" />
我的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;
} }
我在"Start" to "end"
主要活动中更改了.xml
,并且java
主要活动被强行关闭了!
我将“开始”更改为“右”以强制关闭!!
我使用RTL library
但又被迫关闭!!!
提前致谢。
答案 0 :(得分:0)
问题是您的自定义重力未定义并返回NULL。
首先,您应该更新menu.xml
并将项目构建为右侧菜单的图标,如下所示:
<item
android:id="@+id/menu_right"
android:orderInCategory="100"
android:showAsAction="always"
android:icon="@android:drawable/ic_dialog_dialer"
android:title="@string/action_menuright"
/>
在main_activity.java
中禁用从左侧打开的默认切换抽屉并更新:
toggle.syncState();
toggle.setDrawerIndicatorEnabled(false);//this is added line
和类似的更新这个:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
< here: add this line: >
DrawerLayout drawer;
最后将此课程更改为:
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);
}
现在一切都很好。