如何在充气视图中显示App栏和导航抽屉

时间:2018-02-22 19:00:15

标签: java android navigation-drawer toolbar layout-inflater

试图找出如何在充气视图中显示应用栏和导航抽屉。 我正在使用我启动应用程序时预先安装的基本方法。所以这一切都是使用contentmain.xml

从我的主要活动中运行的

但我现在必须对我的主要活动使用不同的观点。当他们膨胀我仍然希望能够使用应用程序栏和导航抽屉。所以我做了我自己的导航抽屉xml称为nav

<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.harrops.h20droidapp2.MainActivity"
android:id="@+id/main"
style="@style/AppBarOverlay">



<android.support.v4.widget.DrawerLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">
    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/appbar"
        />


    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

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

和单独的app_bar_main.xml

RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="@+id/maintoolbar">

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

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

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

<FrameLayout
    android:id="@+id/frame"
    android:layout_width="match_parent"
    android: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 个答案:

答案 0 :(得分:1)

谢谢arron这是我的答案。  制作一项名为基础活动的活动。然后在xml里面放置抽屉布局和工具栏

<android.support.v4.widget.DrawerLayout android:id="@+id/activity_container"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:fitsSystemWindows="true"
tools:openDrawer="start"
>

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

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


        />
</FrameLayout>

</android.support.design.widget.AppBarLayout>
<android.support.design.widget.NavigationView
    android:id="@+id/navigationView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer"/>

然后在baseactivity java里面放置这个

 }
@Override
public void setContentView(int layoutResID)
{
    /**
     * This is going to be our actual root layout.
     */
    fullLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_base, null);
    /**
     * {@link FrameLayout} to inflate the child's view. We could also use a {@link android.view.ViewStub}
     */
    FrameLayout activityContainer = (FrameLayout) fullLayout.findViewById(R.id.activity_content);
    getLayoutInflater().inflate(layoutResID, activityContainer, true);

    /**
     * Note that we don't pass the child's layoutId to the parent,
     * instead we pass it our inflated layout.
     */
    super.setContentView(fullLayout);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    navigationView = (NavigationView) findViewById(R.id.navigationView);

    if (useToolbar())
    {
        SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(this);
        boolean customBackground = sharedpreferences.getBoolean("customBackground",false);
        if(customBackground==true){

            toolbar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.altbuttonFocused)));
        }
        setSupportActionBar(toolbar);
    }
    else
    {
        toolbar.setVisibility(View.GONE);
    }

    setUpNavView();
}

/**
 * Helper method that can be used by child classes to
 * specify that they don't want a {@link Toolbar}
 * @return true
 */
protected boolean useToolbar()
{
    return true;
}

protected void setUpNavView()
{
    navigationView.setNavigationItemSelectedListener(this);

    if( useDrawerToggle()) { // use the hamburger menu
        drawerToggle = new ActionBarDrawerToggle(this, fullLayout, toolbar,
                R.string.nav_drawer_opened,
                R.string.nav_drawer_closed);

        fullLayout.setDrawerListener(drawerToggle);
        drawerToggle.syncState();
    } else if(useToolbar() && getSupportActionBar() != null) {
        // Use home/back button instead
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }
}

/**
 * Helper method to allow child classes to opt-out of having the
 * hamburger menu.
 * @return
 */
protected boolean useDrawerToggle()
{
    return true;
}


@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
    fullLayout.closeDrawer(GravityCompat.START);
    selectedNavItemId = menuItem.getItemId();

    return onOptionsItemSelected(menuItem);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
        if (id ==R.id.nav_settings){
            startActivity(new Intent(this,SettingsActivity.class));
            finish();
        }

        }

然后在任何我想使用工具栏和导航视图的活动中我只是扩展BaseActivity