在所有活动问题中显示导航抽屉
Posted
技术标签:
【中文标题】在所有活动问题中显示导航抽屉【英文标题】:Displaying Navigation drawer in all activities problem 【发布时间】:2020-04-20 11:54:58 【问题描述】:我正在制作一个包含不同活动的导航抽屉,我想在所有活动中显示相同的导航抽屉,我采用相同的代码并将其放在所有活动中,但是当我点击活动时,我的应用程序一直崩溃我不知道为什么
【问题讨论】:
您能分享您的代码以便我们提供帮助吗?无论如何,下面是可能有帮助的示例。 【参考方案1】:尝试下面的代码在所有活动上显示导航抽屉,希望这个示例帮助...!
基础活动
public class BaseActivity extends AppCompatActivity
ArrayList<String> menuList;
protected FrameLayout frameLayout;
protected ListView mDrawerList;
protected static int position;
private static boolean isLaunch = true;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
@SuppressLint("ResourceAsColor")
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.navigation_drawer_base_layout);
prepareListItems();
frameLayout = findViewById(R.id.content_frame);
mDrawerLayout = findViewById(R.id.drawer_layout);
mDrawerList = findViewById(R.id.left_drawer);
View header = getLayoutInflater().inflate(R.layout.header, null);
mDrawerList.addHeaderView(header);
mDrawerList.setAdapter(new ArrayAdapter<>(this, R.layout.drawer_list_item, menuList));
mDrawerList.setOnItemClickListener(new OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
openActivity(position);
);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.menu);
actionBarDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.mipmap.ic_launcher_round, /* nav drawer image to replace 'Up' caret */
R.string.open_drawer, /* "open drawer" description for accessibility */
R.string.close_drawer) /* "close drawer" description for accessibility */
@Override
public void onDrawerClosed(View drawerView)
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
super.onDrawerClosed(drawerView);
@Override
public void onDrawerOpened(View drawerView)
Objects.requireNonNull(getSupportActionBar()).setTitle(getString(R.string.app_name));
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
super.onDrawerOpened(drawerView);
@Override
public void onDrawerSlide(View drawerView, float slideOffset)
super.onDrawerSlide(drawerView, slideOffset);
@Override
public void onDrawerStateChanged(int newState)
super.onDrawerStateChanged(newState);
;
mDrawerLayout.setDrawerListener(actionBarDrawerToggle);
if (isLaunch)
isLaunch = false;
openActivity(1);
private void prepareListItems()
menuList = new ArrayList<>();
menuList.add(" Activity1 ");
menuList.add(" Activity2 ");
menuList.add(" Activity3 ");
menuList.add(" Activity4 ");
/**
* @param position Launching activity when any list item is clicked.
*/
protected void openActivity(int position)
mDrawerList.setItemChecked(position - 1, true);
mDrawerLayout.closeDrawer(mDrawerList);
BaseActivity.position = position; //Setting currently selected position in this field so that it will be available in our child activities.
switch (position)
case 0:
break;
case 1:
startActivity(new Intent(this, Activity1.class));
break;
case 2:
startActivity(new Intent(this, Activity2.class));
break;
case 3:
startActivity(new Intent(this, Activity3.class));
break;
case 4:
startActivity(new Intent(this, Activity4.class));
break;
default:
break;
// Toast.makeText(this, "Selected Item Position::" + position, Toast.LENGTH_LONG).show();
@Override
public boolean onCreateOptionsMenu(Menu menu)
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
@Override
public boolean onOptionsItemSelected(MenuItem item)
if (actionBarDrawerToggle.onOptionsItemSelected(item))
return true;
switch (item.getItemId())
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu)
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
活动1
public class Activity1 extends BaseActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.activity_main, frameLayout);
mDrawerList.setItemChecked(position, true);
@Override
protected void onDestroy()
super.onDestroy();
navigation_drawer_base_layout
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_
android:layout_>
<FrameLayout
android:id="@+id/content_frame"
android:layout_
android:layout_ />
<ListView
android:id="@+id/left_drawer"
android:layout_
android:layout_
android:layout_gravity="start"
android:background="@drawable/simple_brushed_metal"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</androidx.drawerlayout.widget.DrawerLayout>
标题
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_
android:layout_
android:background="@drawable/shade">
<LinearLayout
android:layout_
android:layout_
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_
android:layout_
android:layout_marginTop="5dp"
android:fontFamily="serif"
android:gravity="center"
android:text="@string/app_name"
android:textColor="@color/textColorPrimary"
android:textSize="18sp" />
<TextView
android:layout_
android:layout_
android:gravity="center"
android:text="@string/developed_by_nd_software_solution"
android:textColor="@color/textColorPrimary"
android:textSize="12sp" />
<View
android:layout_
android:layout_
android:layout_marginTop="8dp"
android:background="@color/textColorPrimary" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
drawer_list_item
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_
android:layout_
android:background="?android:attr/activatedBackgroundIndicator"
android:fontFamily="serif"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="@color/textColorPrimary" />
activity_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_
android:layout_
android:background="@color/colorPrimary">
<TextView
android:layout_
android:layout_
android:layout_marginTop="5dp"
android:fontFamily="serif"
android:gravity="center"
android:text="@string/app_name"
android:textColor="@color/textColorPrimary"
android:textSize="18sp" />
</androidx.constraintlayout.widget.ConstraintLayout>
这里我为一个活动(Activity1)提供了代码,为其他活动(如 Activity2、Activity3 等)编写代码。
【讨论】:
以上是关于在所有活动问题中显示导航抽屉的主要内容,如果未能解决你的问题,请参考以下文章
想要使用片段从导航抽屉活动移动到另一个屏幕,以在所有屏幕上显示抽屉