2017-03-05 106 views
-1

我有一些圖像按鈕和滾動視圖「主要活動」的活動,並且我從android studio添加了另一個「導航抽屜」活動。 如何將「導航抽屜」添加到我目前的「主要活動」中? 我試圖複製我的主要活動內的代碼,沒有錯誤,但應用程序崩潰。有人建議做MainAcitivty擴展NavActivity,但它不起作用。 請提供任何提示或想法?Android Studio,爲我當前的活動分配一個導航抽屜活動

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)); 

    }} 
+0

你可以分享你的代碼或logcat的? –

+0

如何不使用模板,只是學習如何將DrawerLayout + NavigationView集成到代碼中?如果您有錯誤,請顯示您的代碼並詢問有關錯誤。換句話說,如果應用程序崩潰,是的,有錯誤 –

+0

你好,我已經添加了我的主要活動的代碼,我想整合抽屜在該特定的活動,或所有其他人,我必須手動實現它? – Lalati

回答

1

試試這個: 第1步: 我們需要添加依賴的設計支持庫。添加以下依賴項。

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

+0

謝謝,我已經按照教程「https://www.youtube.com/watch?v=AS92bq3XxkA」的步驟是有點相似!很高興它的工作 – Lalati