有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

    多亏了阿伦,这是我的答案。 做一个叫做基本活动的活动。然后在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