java如何在充气视图中显示应用程序栏和导航抽屉
试图找出如何在膨胀视图中显示应用程序栏和导航抽屉。 我使用的是我启动应用程序时预装的基本方法。所以这一切都是从我使用contentmain的主要活动开始的。xml
但我现在不得不对我的主要活动有不同的看法。当它们膨胀时,我仍然希望能够使用应用程序栏和导航抽屉。所以我制作了自己的nav抽屉xml,名为nav
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context="com.example.harrops.h20droidapp2.MainActivity"
安卓:id="@+id/main"
style="@style/AppBarOverlay">
<安卓.support.v4.widget.DrawerLayout
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:id="@+id/drawer_layout"
安卓:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:id="@+id/appbar"
/>
<安卓.support.design.widget.NavigationView
安卓:id="@+id/nav_view"
安卓:layout_width="wrap_content"
安卓:layout_height="match_parent"
安卓:layout_gravity="start"
安卓:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</安卓.support.v4.widget.DrawerLayout>
还有一个独立的应用程序。xml
RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:fitsSystemWindows="true"
安卓:id="@+id/maintoolbar">
<安卓.support.design.widget.AppBarLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:theme="@style/AppBarOverlay">
<安卓.support.v7.widget.Toolbar
安卓:id="@+id/toolbar"
安卓:layout_width="match_parent"
安卓:layout_height="?attr/actionBarSize"
安卓:background="@color/colorPrimary"
app:popupTheme="@style/PopupOverlay"
app:title="H20 Droid App "
/>
</安卓.support.design.widget.AppBarLayout>
<FrameLayout
安卓:id="@+id/frame"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"></FrameLayout>
我正努力让它展现出来
sharedpreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean layout_alt = sharedpreferences.getBoolean("alt_layout",false);
if (layout_alt==true){
loadVideoAdvert();
LayoutInflater layoutInflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.main_alt, null);
RelativeLayout rl = (RelativeLayout)findViewById(R.id.rlmain);
tb = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(tb);
View nav;
nav = layoutInflater.inflate(R.layout.nav,null);
DrawerLayout drawer = (DrawerLayout) nav.findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, tb, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView)nav. findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
mAdView = (AdView) view.findViewById(R.id.adView);
mAdView.setAdSize(AdSize.BANNER);
mAdView.setAdUnitId("myadviewid");
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
rl.addView(view);
# 1 楼答案
多亏了阿伦,这是我的答案。 做一个叫做基本活动的活动。然后在xml中放置抽屉布局和工具栏
然后在baseactivity java中放置
然后在任何我想使用工具栏和导航视图的活动中,我只是扩展了BaseActivity