【问题标题】:Navigation Drawer is opened on activity start导航抽屉在活动开始时打开
【发布时间】:2019-05-05 18:10:07
【问题描述】:

我的导航抽屉在应用程序启动时打开,我找不到让它进入的方法,即使按下后退按钮也不行,如何让它在活动开始时不打开,以及在滑动手势上隐藏它?

这是我在显示导航抽屉的活动上的代码。

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>

预计会在向左滑动手势时打开,而不是在活动开始时开始,我们将不胜感激。

【问题讨论】:

  • DrawerLayout 不会自行打开抽屉。你确定你在某个地方没有给openDrawer()打电话吗?此外,根据您对背压的描述,听起来您可能在 app_bar_nav_drawer 布局中的某处有另一个 NavigationView,或者在其中的另一个布局 &lt;include&gt;d 中。您显示的布局是正确的,所以它不应该是那个。如果从边缘滑动会发生什么?另一个抽屉是否在已经存在的抽屉上打开?

标签: android


【解决方案1】:

请添加此属性:

<android.support.v4.widget.DrawerLayout
 ....
 android:gravity="right"
 ....>

【讨论】:

  • 首先,谢谢你的回答,它不起作用,我的导航抽屉仍然在我的活动开始时弹出。
  • 另外,我什至无法将“android:gravity="right" 添加到该小部件。
  • 为什么不能?我在我的抽屉属性中使用它
  • 我希望你能告诉我那个人,它只是看起来不可用,我可以使用的唯一可用代码行是“android:layout_gravitiy="right"。
  • 相信我我正在使用它,你的情况很奇怪,但无论如何我只是想帮忙!
【解决方案2】:

尝试在 Activity 的 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 了解更多信息)

【讨论】:

  • 我将这些代码行添加到我的 onCreate() 代码块中,但我得到了“(这个,抽屉,工具栏,“抽屉已打开”,“抽屉已关闭”)“带有红色下划线.
  • @TomasMota 好吧,我没有澄清,因为我认为它是隐含的……mDrawer 是一个变量,它包含对您的 DrawerLayout 的引用和 mToolbar 一个指向您的变量工具栏。因此,您必须创建这两个变量并使用 findViewById(或 ButterKnife 等库)获取引用。
  • 这里说“ActionBarDrawerToggle”不能应用于“工具栏”和两个字符串。
  • @TomasMota 您将不得不使用正在尝试的完整代码来更新您的问题,否则我将无法提供帮助
  • 好吧,你去吧,我的完整课程代码,所以我试图阻止我的导航抽屉在活动开始时打开,我不知道是哪一行代码使它打开.我还想添加一些代码块以使其在向左滑动手势时打开/关闭。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-12
  • 2017-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多