Android Studio,正在为我当前的 activity 分配导航抽屉 activity
Android Studio , Assigning a Navigation Drawer activity to my current activity
我有一个 activity,带有一些图像按钮和滚动视图“Main Activity”,我从 [=20= 添加了另一个 "Navigation Drawer" activity ] 工作室。
如何将 "Navigation Drawer" 添加到我当前的 "Main Activity" ?
我已经尝试复制我的主 activity 中的代码,没有错误,但是应用程序 crashes.Some 人们建议执行 MainAcitivty extends 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));
}}
去学习如何创建导航抽屉:
https://developer.android.com/training/implementing-navigation/nav-drawer.html
试试这个:
步骤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 中 class
粘贴它并在 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
我有一个 activity,带有一些图像按钮和滚动视图“Main Activity”,我从 [=20= 添加了另一个 "Navigation Drawer" activity ] 工作室。 如何将 "Navigation Drawer" 添加到我当前的 "Main Activity" ? 我已经尝试复制我的主 activity 中的代码,没有错误,但是应用程序 crashes.Some 人们建议执行 MainAcitivty extends 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));
}}
去学习如何创建导航抽屉: https://developer.android.com/training/implementing-navigation/nav-drawer.html
试试这个: 步骤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 中 class
粘贴它并在 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