我的导航抽屉在应用启动时打开,我什至找不到方法,即使按下后退按钮也不行,如何在活动启动时使其不打开,以及以滑动手势隐藏它?
这是我在显示导航抽屉时所具有的代码。
public class BottomActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
RadioGroup radioGroup;
RadioButton Rd1, Rd2, Rd3, Rd4;
DrawerLayout drawer;
ImageView ProfilePic;
@SuppressLint({"RtlHardcoded", "ClickableViewAccessibility"})
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_bottom );
Objects.requireNonNull( getSupportActionBar() ).setDisplayOptions( ActionBar.DISPLAY_SHOW_CUSTOM );
getSupportActionBar().setCustomView( R.layout.abs_layout_home );
radioGroup = findViewById( R.id.radioGroup );
Rd1 = findViewById( R.id.radioButton );
Rd2 = findViewById( R.id.radioButton2 );
Rd3 = findViewById( R.id.radioButton3 );
Rd4 = findViewById( R.id.radioButton4 );
radioGroup.setOnCheckedChangeListener( new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (Rd1.isChecked()) {
Intent intent = new Intent( getApplicationContext(), BottomActivity.class );
startActivity( intent );
}
if (Rd2.isChecked()) {
Intent intent1 = new Intent( getApplicationContext(), DashBoard.class );
startActivity( intent1 );
}
if (Rd3.isChecked()) {
Intent intent2 = new Intent( getApplicationContext(), SettingsActivity.class );
startActivity( intent2 );
} else {
if (Rd4.isChecked()) {
Intent intent3 = new Intent( getApplicationContext(), Messages.class );
startActivity( intent3 );
}
}
}
} );
Toolbar Toolbar = findViewById( R.id.toolbar );
DrawerLayout Drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, Drawer, Toolbar, "Drawer is opened", "Drawer is closed");
Drawer.addDrawerListener(toggle);
toggle.syncState();
}
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen( GravityCompat.START )) {
drawer.closeDrawer( GravityCompat.START );
} else {
super.onBackPressed();
}
}
@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(@NonNull MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_profile) {
startActivity( new Intent( BottomActivity.this, ProfileActivity.class ) );
} else if (id == R.id.nav_settings) {
startActivity( new Intent( BottomActivity.this, SettingsActivity.class ) );
} else if (id == R.id.nav_share) {
}
return true;
}
public void onClickButtonListener() {
ProfilePic = findViewById( R.id.ProfilePicture );
ProfilePic.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent( BottomActivity.this, ProfileActivity.class );
startActivity( intent );
}
} );
}
}
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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">
<include
layout="@layout/app_bar_nav_drawer"
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"
app:headerLayout="@layout/nav_header_nav_drawer"
app:menu="@menu/activity_nav_drawer_drawer" />
</android.support.v4.widget.DrawerLayout>
预计它会在向左滑动手势时打开,而不是在活动开始时启动,因此将不胜感激。
答案 0 :(得分:0)
请添加此属性:
<android.support.v4.widget.DrawerLayout
....
android:gravity="right"
....>
答案 1 :(得分:0)
尝试在“活动”的onCreate()
方法上添加ActionBarDrawerToggle。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, mDrawer, mToolbar, "Drawer is opened", "Drawer is closed");
mDrawer.addDrawerListener(toggle);
toggle.syncState();
...
}
这可以解决问题,并为您处理显示/隐藏操作。您仍然可以在没有ActionBarDrawerToggle
的情况下完成此任务,但是我认为这样做不值得(请查看this question and its answers以获得更多信息)