Android Studio,为我当前的活动分配导航抽屉活动

时间:2017-03-05 16:47:47

标签: android navigationbar

我有一些活动,有一些图像按钮和滚动视图“主要活动”,我已经从android工作室添加了另一个“导航抽屉”活动。 如何将“导航抽屉”添加到我当前的“主要活动”中? 我试图复制我的主要活动中的代码,没有错误,但应用程序崩溃。有些人建议做MainAcitivty扩展NavActivity但它不起作用。 有什么提示或想法吗?

 public class LoginSuccessActivity extends Activity
     {
    private Button logoutButton;


    public void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );
    setContentView( R.layout.login_success );



    ImageButton simpleImageButton =     (ImageButton)findViewById(R.id.imageButton2);
    simpleImageButton.setImageResource(R.drawable.i4);
    simpleImageButton.setBackgroundColor(Color.TRANSPARENT);

    ImageButton simpleImageButton1 = (ImageButton)findViewById(R.id.imageButton3);
    simpleImageButton1.setImageResource(R.drawable.i3);
    simpleImageButton1.setBackgroundColor(Color.TRANSPARENT);

    ImageButton simpleImageButton2 = (ImageButton)findViewById(R.id.imageButton4);
    simpleImageButton2.setImageResource(R.drawable.i2);
    simpleImageButton2.setBackgroundColor(Color.TRANSPARENT);

    ImageButton simpleImageButton3 = (ImageButton)findViewById(R.id.imageButton5);
    simpleImageButton3.setImageResource(R.drawable.i1);
    simpleImageButton3.setBackgroundColor(Color.TRANSPARENT);

    initUI();

  }

  private void initUI()
  {

  }

       private void onLogoutButtonClicked()
       {
       Backendless.UserService.logout( new DefaultCallback<Void>( this )
       {
         @Override
         public void handleResponse( Void response )
         {
           super.handleResponse( response );
          startActivity( new Intent( LoginSuccessActivity.this,  LoginActivity.class ) );
          finish();
        }

       @Override
       public void handleFault( BackendlessFault fault )
         {
          if( fault.getCode().equals( "3023" ) ) // Unable to logout: not logged in (session expired, etc.)
          handleResponse( null );
        else
          super.handleFault( fault );
      }
    } );

     }
      public void profile(View v){
      startActivity(new Intent(LoginSuccessActivity.this, test.class));

    }}

2 个答案:

答案 0 :(得分:1)

试试这个: 步骤1: 我们需要为Design Support库添加依赖项。添加以下依赖项。

compile 'com.android.support:design:23.2.0'

第2步: 在菜单文件夹

中创建menu_navigation.xml文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/home"
            android:title="Home"
            android:icon="@drawable/ic_home"/>
        <item
            android:id="@+id/settings"
            android:title="Settings"
            android:icon="@drawable/ic_setting"/>
        <item
            android:id="@+id/trash"
            android:title="Trash"
            android:icon="@drawable/ic_trash"/>
        <item
            android:id="@+id/logout"
            android:title="Logout"
            android:icon="@drawable/ic_exit"/>
    </group>
</menu>

第3步: 在布局文件夹

中创建nav_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:paddingTop="20dp"
    android:paddingBottom="20dp"
    android:background="@color/colorPrimaryDark"
    android:layout_width="match_parent"
    android:layout_height="190dp">

    <ImageView
        android:src="@drawable/ic_person"
        android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:layout_height="0dp" />
    <TextView
        android:id="@+id/tv_email"
        android:textColor="@color/White"
        android:textSize="18sp"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

第4步: 在您的activity_main.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context="com.learn2crack.myapplication.MainActivity">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.AppBarLayout>

        <include layout="@layout/Call Your Layout" />

    </android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/menu_navigation"/>

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

第5步:

<string name="drawer_open">Open</string>
<string name="drawer_close">Close</string>

第6步:

在您的MainActivity类中

将其粘贴并在onCreate中调用。

 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.home:
                        Toast.makeText(getApplicationContext(),"Home",Toast.LENGTH_SHORT).show();
                        drawerLayout.closeDrawers();
                        break;
                    case R.id.settings:
                        Toast.makeText(getApplicationContext(),"Settings",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.trash:
                        Toast.makeText(getApplicationContext(),"Trash",Toast.LENGTH_SHORT).show();
                        drawerLayout.closeDrawers();
                        break;
                    case R.id.logout:
                        finish();

                }
                return true;
            }
        });
        View header = navigationView.getHeaderView(0);
        TextView tv_email = (TextView)header.findViewById(R.id.tv_email);
        tv_email.setText("Any String");
        drawerLayout = (DrawerLayout)findViewById(R.id.drawer);

        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close){

            @Override
            public void onDrawerClosed(View v){
                super.onDrawerClosed(v);
            }

            @Override
            public void onDrawerOpened(View v) {
                super.onDrawerOpened(v);
            }
        };
        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();
    }

更多详情请参阅本网站 https://www.learn2crack.com/2016/03/android-material-design-sliding-navigation-drawer.html

答案 1 :(得分:0)