Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 导航抽屉在活动启动时打开_Android - Fatal编程技术网

Android 导航抽屉在活动启动时打开

Android 导航抽屉在活动启动时打开,android,Android,我的导航抽屉在应用程序启动时打开,我找不到方法让它进入,即使按下了后退按钮,我如何使它在活动启动时不打开,并在滑动手势时隐藏它 这是我在显示导航抽屉的活动上的代码 public class BottomActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { RadioGroup radioGroup; RadioButton Rd1, Rd2, Rd

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

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

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>


预计它将以向左滑动的姿势打开,而不是在活动开始时启动,任何帮助都将不胜感激。

请添加此属性:

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

尝试在活动的
onCreate()
方法中添加

@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
,您仍然可以实现这一点,但我认为这不值得付出努力(查看更多信息)

首先,感谢您的回答,它不起作用,我的导航抽屉仍然会在活动开始时弹出。此外,我甚至不能添加“android:gravity=”正确“给那个小部件。为什么你不能?我在我的抽屉里使用它,我希望你能告诉我,这个人,它看起来不可用,我唯一可用的代码行是“android:layout_Gravity=“right”。相信我,我在使用它,对你来说很奇怪,但无论如何,我只是想帮你!我将这些代码行添加到了我的onCreate()代码块中,但我得到了“(此,抽屉,工具栏,“抽屉已打开”,“抽屉已关闭”)“红色下划线。@TomasMota我没有澄清,因为我认为这是隐含的…
mDrawer
是一个变量,它包含对
DrawerLayout
的引用,而
mToolbar
是一个指向工具栏的变量。因此,您必须创建这两个变量,并使用
findViewById
(或者像ButterKnife这样的库)。这里说“ActionBarDrawerToggle”不能应用于“toolbar”还有两个字符串。@TomasMota你必须用你正在尝试的内容的完整代码更新你的问题,我不可能帮助其他人好了,伙计,你看,我的完整类代码,所以我试图阻止我的导航抽屉在活动开始时打开,我不知道是哪行代码打开的。我也会要添加一些代码块,以使其在向左滑动时打开/关闭。
DrawerLayout
无法单独打开其抽屉。是否确实没有调用
openDrawer()
某个地方?另外,从您对背压的描述来看,您可能在
应用程序工具栏导航抽屉的某个地方有另一个
导航视图
布局,或者在其中的另一个布局
中。您显示的布局是正确的,因此不应该是该布局。如果您从边缘滑动会发生什么?不会她的抽屉在已经在那里的抽屉上打开了?