如何在膨胀视图中显示应用栏和导航抽屉

马克哈罗普

试图弄清楚如何在膨胀的视图中显示应用栏和导航抽屉。我使用的是在我启动应用程序时预先安装的基本方法。所以这一切都使用 contentmain.xml 从我的主要活动中运行

但我现在必须对我的主要活动使用不同的观点。当它们膨胀时,我仍然希望能够使用应用栏和导航抽屉。所以我制作了我自己的导航抽屉 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"
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);
马克哈罗普

感谢 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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在许多视图中显示相同的导航栏?

来自分类Dev

如何在许多视图中显示相同的导航栏?

来自分类Dev

在抽屉和导航视图中重用工具栏布局

来自分类Dev

如何在抽屉布局的导航视图中设置这些彩色图标

来自分类Dev

如何在导航标题视图中居中显示文本

来自分类Dev

如何在所有视图中显示标签栏

来自分类Dev

iOS-表格视图中的视图不会显示导航栏

来自分类Dev

如何在导航栏应用程序中显示网页?

来自分类Dev

菜单项之后如何在导航抽屉中显示文本视图?

来自分类Dev

导航抽屉和底部栏

来自分类Dev

导航抽屉和底部栏

来自分类Dev

如何使用Android在应用栏下方创建裁剪的导航抽屉?

来自分类Dev

在所有视图中显示粘性导航栏

来自分类Dev

如何在带有导航栏的模式视图中更改iOS 7中的UIStatusBarStyle?

来自分类Dev

我在哪里/如何在yii视图中包含导航栏

来自分类Dev

如何在带有导航栏的模式视图中更改iOS 7中的UIStatusBarStyle?

来自分类Dev

如何在网络视图中显示的Android wordpress客户端应用中使Youtube和Facebook视频全屏显示

来自分类Dev

列表视图和导航抽屉

来自分类Dev

如何在视图中自动加载和显示图像(Codeigniter)

来自分类Dev

如何在 Laravel 视图中显示 DBMS 名称和版本?

来自分类Dev

如何在视图中设计标签栏

来自分类Dev

如何在iOS中推送整个视图,包括导航栏和工具栏

来自分类Dev

如何在导航抽屉中获取项目以更改视图

来自分类Dev

如何在导航抽屉中使用视图寻呼机?

来自分类Dev

如何在导航抽屉中获取项目以更改视图

来自分类Dev

如何在导航抽屉中使用视图寻呼机?

来自分类Dev

如何在带有工具栏和底部导航视图的布局中使用滚动视图?

来自分类Dev

如何仅在移动视图中使导航栏折叠

来自分类Dev

文本未出现在Android导航抽屉应用程序的“片段”视图中

Related 相关文章

  1. 1

    如何在许多视图中显示相同的导航栏?

  2. 2

    如何在许多视图中显示相同的导航栏?

  3. 3

    在抽屉和导航视图中重用工具栏布局

  4. 4

    如何在抽屉布局的导航视图中设置这些彩色图标

  5. 5

    如何在导航标题视图中居中显示文本

  6. 6

    如何在所有视图中显示标签栏

  7. 7

    iOS-表格视图中的视图不会显示导航栏

  8. 8

    如何在导航栏应用程序中显示网页?

  9. 9

    菜单项之后如何在导航抽屉中显示文本视图?

  10. 10

    导航抽屉和底部栏

  11. 11

    导航抽屉和底部栏

  12. 12

    如何使用Android在应用栏下方创建裁剪的导航抽屉?

  13. 13

    在所有视图中显示粘性导航栏

  14. 14

    如何在带有导航栏的模式视图中更改iOS 7中的UIStatusBarStyle?

  15. 15

    我在哪里/如何在yii视图中包含导航栏

  16. 16

    如何在带有导航栏的模式视图中更改iOS 7中的UIStatusBarStyle?

  17. 17

    如何在网络视图中显示的Android wordpress客户端应用中使Youtube和Facebook视频全屏显示

  18. 18

    列表视图和导航抽屉

  19. 19

    如何在视图中自动加载和显示图像(Codeigniter)

  20. 20

    如何在 Laravel 视图中显示 DBMS 名称和版本?

  21. 21

    如何在视图中设计标签栏

  22. 22

    如何在iOS中推送整个视图,包括导航栏和工具栏

  23. 23

    如何在导航抽屉中获取项目以更改视图

  24. 24

    如何在导航抽屉中使用视图寻呼机?

  25. 25

    如何在导航抽屉中获取项目以更改视图

  26. 26

    如何在导航抽屉中使用视图寻呼机?

  27. 27

    如何在带有工具栏和底部导航视图的布局中使用滚动视图?

  28. 28

    如何仅在移动视图中使导航栏折叠

  29. 29

    文本未出现在Android导航抽屉应用程序的“片段”视图中

热门标签

归档