导航抽屉意图活动Android Studio

时间:2017-02-11 19:58:26

标签: android android-studio android-intent

我为App创建了一个导航抽屉,我正在学习代码和所有内容。我已经到了我无法在导航抽屉工作中与其他活动建立任何链接的地步。

主要活动

public class MainActivity2 extends AppCompatActivity {

private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    final Button bCreateTask = (Button) findViewById(R.id.btnCreateNewTask);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.DrawerLayout);
    mToggle = new ActionBarDrawerToggle(this,mDrawerLayout, R.string.Open,      R.string.Close);

    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    bCreateTask.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity2.this,                                                   NewTaskActivity.class);
            MainActivity2.this.startActivity(intent);

        }
    });
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(mToggle.onOptionsItemSelected(item)){
        return true;

    }
    return super.onOptionsItemSelected(item);
}




}

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout     xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.rikhi.chores.MainActivity2">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <Button
        android:text="New Task"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnCreateNewTask"
        android:layout_weight="1" />
</LinearLayout>

<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:menu="@menu/navigation_menu"
    android:layout_gravity="start"
    app:headerLayout="@layout/navigation_header">
</android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

Navigation_Menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/nav_accont"
    android:icon="@mipmap/ic_person_black_24dp"
    android:title="MyAccount"/>
<item android:id="@+id/nav_setting"
    android:icon="@mipmap/ic_settings_black_24dp"
    android:title="settings"/>
<item android:id="@+id/nav_logout"
    android:icon="@mipmap/ic_lock_outline_black_24dp"
    android:title="logout"/>


</menu>

我不确定我是否必须使用意图或它是如何运作的,如果有人可以指导我那将是伟大的。如果需要任何其他代码,请询问并且很乐意提供。

3 个答案:

答案 0 :(得分:0)

{{1}}

答案 1 :(得分:0)

如果你想添加抽屉,那么你必须使用这种方式。

     @SuppressWarnings("StatementWithEmptyBody")
     @Override
     public boolean onNavigationItemSelected(MenuItem item) {
      int id = item.getItemId();

      if (id == R.id.nav_home) {

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

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

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

答案 2 :(得分:0)

试试这个:

Arrays

同样在你的布局中:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    initNavigationDrawer();

    final Button bCreateTask = (Button) findViewById(R.id.btnCreateNewTask);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.DrawerLayout);
    mToggle = new ActionBarDrawerToggle(this,mDrawerLayout, R.string.Open,R.string.Close);

    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    bCreateTask.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity2.this,NewTaskActivity.class);
            MainActivity2.this.startActivity(intent);

        }
    });
}

public void initNavigationDrawer() {

    NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {

            int id = menuItem.getItemId();

            switch (id) {
                case R.id.nav_accont:
                    Intent intent = new Intent(MainActivity2.this, SecondActivity.class);
                    startActivity(intent);
                    break;
                case R.id.nav_setting:
                    //same logic
                    break;
                case R.id.nav_logout:
                   //same logic
                    break;
                default:
                    break;
            }
            return true;
        }
    });
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(mToggle.onOptionsItemSelected(item)){
        return true;

    }
    return super.onOptionsItemSelected(item);
}