操作栏抽屉切换按钮未显示第二个活动

时间:2017-03-05 15:11:19

标签: android android-actionbar actionbardrawertoggle

我在Android应用程序中有以下两项活动:

1)LevelSelectActivity (activity_level_select.xml)

2)OptionsQuizActivity (activity_options_quiz.xml)

第一个活动调用第二个活动。我为这两个活动设置了导航抽屉,并带有自定义操作栏布局。

我想要的是什么:

带有导航栏抽屉切换按钮的两个活动的透明操作栏。操作栏应覆盖下面的布局。操作栏上只能显示切换按钮。

什么有效:

活动1正确显示操作栏,透明背景覆盖以下布局和切换按钮。所以一切都按活动1的预期工作。

什么不起作用:

活动2没有显示操作栏,有趣的部分是我使用相同的java代码,操作栏布局和两种活动的样式。 任何人都可以检查我的代码,找出问题所在?我把头发拉过头发..

  

活动1布局文件:activity_level_select.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout_level"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    android:background="#4285f4"
    tools:context=".ui.LevelSelectActivity"
    android:fitsSystemWindows="true"
    >

    <include
        layout="@layout/custom_navigation_bar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="35dp"
            android:paddingRight="35dp"
            android:gravity="center"
            android:layout_centerHorizontal="true"
            android:layout_alignParentTop="true"
            android:layout_above="@+id/linearLayout">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:textSize="31sp"
                android:layout_marginTop="80dp"
                android:text="@string/level_heading"
                android:textColor="#ffffff"
                android:id="@+id/diffiLevelTextView"
                android:fontFamily="sans-serif-light"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:background="@drawable/semi_white_button"
                android:text="@string/button_level_medium"
                android:fontFamily="sans-serif-light"
                android:textSize="22sp"
                android:textAllCaps="false"
                android:textColor="#ffffff"
                android:layout_marginBottom="40dp"
                android:id="@+id/buttonLevelMedium" />




        </LinearLayout>


        <LinearLayout
            android:layout_height="20dp"
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_marginBottom="20dp"
            android:layout_alignParentStart="true"
            android:id="@+id/linearLayout">

            <TextView
                android:layout_width="0dp"
                android:id="@+id/tv_rate"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:textColor="#88ffffff"
                android:textSize="17dp"
                android:text="@string/rate_text"/>


        </LinearLayout>
    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view_level"
        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>
  

活动1 LevelSelectActivity Java代码:从活动的onCreate方法调用prepareActionBar方法

private void prepareActionBar(){
        // Declaring and Setting our custom build navigation bar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //Setting Transparent Color for Action Bar
        ActionBar actionBar = getSupportActionBar();
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
        actionBar.setTitle("");


        //Creating and preparing the Navigation Drawer button
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout_level);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();


        //Setting Navigation Item Listeners
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view_level);
        navigationView.setNavigationItemSelectedListener(this);      
    }
  

活动1屏幕截图:显示透明操作栏以及操作栏切换按钮。

enter image description here

  

活动2布局文件:activity_options_quiz.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    android:background="#4285f4"
    android:id="@+id/drawer_layout_quiz"
    tools:context=".ui.OptionsQuizActivity"
    android:fitsSystemWindows="true">

    <include
        layout="@layout/custom_navigation_bar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <RelativeLayout
        android:id="@+id/mainLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="170dp"
                android:background="#33000000"
                android:id="@+id/darkQuestionBack"
                android:gravity="center">

                <TextView
                    android:id="@+id/qusTextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/test_question"
                    android:paddingLeft="40dp"
                    android:paddingRight="40dp"
                    android:textColor="#aaffffff"
                    android:fontFamily="sans-serif-light"
                    android:textSize="30sp"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:orientation="vertical"
                android:gravity="center"
                android:layout_height="0dp"
                android:paddingLeft="40dp"
                android:paddingRight="40dp">


                <Button
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:text="Option One"
                    android:textColor="#ffffff"
                    android:id="@+id/optionButton0"
                    android:background="@drawable/semi_white_button"
                    android:textSize="24sp"
                    android:textAllCaps="false"
                    android:layout_marginBottom="15dp"
                    android:fontFamily="sans-serif-light" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:text="Option Two"
                    android:textColor="#ffffff"
                    android:id="@+id/optionButton1"
                    android:background="@drawable/semi_white_button"
                    android:textSize="24sp"
                    android:layout_marginBottom="15dp"
                    android:textAllCaps="false"
                    android:fontFamily="sans-serif-light" />

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:text="Option Three"
                    android:textColor="#ffffff"
                    android:id="@+id/optionButton2"
                    android:background="@drawable/semi_white_button"
                    android:textSize="24sp"
                    android:layout_marginBottom="15dp"
                    android:textAllCaps="false"
                    android:fontFamily="sans-serif-light"/>

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:text="Option Four"
                    android:textColor="#ffffff"
                    android:id="@+id/optionButton3"
                    android:background="@drawable/semi_white_button"
                    android:textSize="24sp"
                    android:layout_marginBottom="15dp"
                    android:textAllCaps="false"
                    android:fontFamily="sans-serif-light"/>


            </LinearLayout>

            <com.google.android.gms.ads.AdView
                android:id="@+id/adView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                ads:adSize="SMART_BANNER"
                ads:adUnitId="@string/banner_ad_unit_id">
            </com.google.android.gms.ads.AdView>

        </LinearLayout>
    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view_quiz"
        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>
  

活动2 JavaCode:OptionsQuizActivity

private void prepareActionBar(){
        // Declaring and Setting our custom build navigation bar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //Setting Transparent Color for Action Bar
        ActionBar actionBar = getSupportActionBar();
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));


        //Creating and preparing the Navigation Drawer button
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout_quiz);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer,toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        //Setting Navigation Item Listeners
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view_quiz);
        navigationView.setNavigationItemSelectedListener(this);

    }
  

活动2 ScreenShot:不显示操作栏切换按钮

enter image description here

  

自定义操作栏布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp"
        android:background="#00000000"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/transparent"
            app:elevation="0dp"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>



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

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

Styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="android:windowActionBarOverlay">true</item>
        <item name="windowActionBarOverlay">true</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <!-- Themes for Navigation Drawer -->
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="background">@android:color/transparent</item>

    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light">
        <item name="windowNoTitle">true</item>
    </style>
</resources>
  

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="sprintingminds.com.countrycapitalquiz">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true">

        <activity
            android:name=".ui.LevelSelectActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".ui.OptionsQuizActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">
        </activity>

        <activity
            android:screenOrientation="portrait"
            android:label="@string/app_name_blank"
            android:name=".ui.IntroController"
            android:theme="@style/NoActionBar"/>
    </application>

</manifest>

感谢您阅读这个长期的问题。请帮帮我:(

更新1:

以下是第二个活动的更多java代码

  

活动2:

package sprintingminds.com.countrycapitalquiz.ui;

import android.animation.Animator;
import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import java.util.Timer;
import java.util.TimerTask;

public class OptionsQuizActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

    private LinearLayout mDarkQuestionBack, mNavBarBack;
    private NavigationView mNavView;
    private View mHeaderView;
    private RelativeLayout mMainLayout;
    private Button mOptionButton0, mOptionButton1, mOptionButton2, mOptionButton3, mGotItButton;
    private String mOption1, mOption2, mOption3, mOption4,mNextQuestion;
    private TextView mQusTextView;
    private QuizGenerator mQuizGenerator;
    private ColorGenerator mColorGenerator;
    private boolean mNextQuestionCounter, mFirstRunCounter;
    private MediaPlayer mMediaCorrect, mMediaWrong;
    private ValueAnimator mBackDarkColorAnimator, mBackLightColorAnimator, mBarColorAnimator ,mQuestionFadeInAnimator, mQuestionFadeOutAnimator;
    private ValueAnimator mButtonFadeOut,mButton1FadeIn, mButton2FadeIn, mButton3FadeIn, mButton4FadeIn;
    private ActionBarDrawerToggle toggle;
    private DrawerLayout drawer;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_options_quiz);


        //Preparing our custom the action bar with Navigation Drawer
        prepareActionBar();


        mNextQuestionCounter = false;
        mFirstRunCounter = true;
        mMediaCorrect = MediaPlayer.create(OptionsQuizActivity.this, R.raw.correct);
        mMediaWrong = MediaPlayer.create(OptionsQuizActivity.this, R.raw.wrong);


        // Initialize Views
        mMainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
        mDarkQuestionBack = (LinearLayout) findViewById(R.id.darkQuestionBack);
        mOptionButton0 = (Button) findViewById(R.id.optionButton0);
        mOptionButton1 = (Button) findViewById(R.id.optionButton1);
        mOptionButton2 = (Button) findViewById(R.id.optionButton2);
        mOptionButton3 = (Button) findViewById(R.id.optionButton3);
        mQusTextView = (TextView) findViewById(R.id.qusTextView);
        mNavView = (NavigationView) findViewById(R.id.nav_view_quiz);
        mHeaderView =  mNavView.getHeaderView(0);
        mNavBarBack = (LinearLayout) mHeaderView.findViewById(R.id.nav_bar_back);


        // Generating New Question and displaying on screen with Random Color
        mQuizGenerator = new QuizGenerator();
        mColorGenerator = new ColorGenerator();
        displayNextQuestion();

        // Giving music control to Volume Rockers
        setVolumeControlStream(AudioManager.STREAM_MUSIC);

        // Initialize Ads
        new Timer().schedule(new TimerTask()
        {
            @Override
            public void run()
            {
                runOnUiThread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        MobileAds.initialize(getApplicationContext(), "ca-app-pub-9601267183367204/3495012579");
                        AdView mAdView = (AdView) findViewById(R.id.adView);
                        AdRequest adRequest = new AdRequest.Builder().build();
                        mAdView.loadAd(adRequest);
                    }
                });
            }
        }, 1000);


        //Setting On Click Listeners
        mOptionButton0.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                checkAnswer(0);
            }
        });

        mOptionButton1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                checkAnswer(1);
            }
        });

        mOptionButton2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                checkAnswer(2);
            }
        });

        mOptionButton3.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                checkAnswer(3);
            }
        });


        mMainLayout.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                if (mNextQuestionCounter){
                    mMediaWrong.stop();
                    mMediaCorrect.stop();
                    mMediaWrong.release();
                    mMediaCorrect.release();
                    mMediaWrong = MediaPlayer.create(OptionsQuizActivity.this,R.raw.wrong);
                    mMediaCorrect = MediaPlayer.create(OptionsQuizActivity.this,R.raw.correct);
                    try {
                        mMediaCorrect.prepare();
                        mMediaWrong.prepare();
                    }catch (Exception e){
                        //do something when unable to play sound
                    }
                    displayNextQuestion();
                    toggleButtons(true);
                    toggleButtonState();
                    mNextQuestionCounter=false;
                }
            }
        });

    }



    private void prepareActionBar(){
        // Declaring and Setting our custom build navigation bar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //Setting Transparent Color for Action Bar
        ActionBar actionBar = getSupportActionBar();
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));


        //Creating and preparing the Navigation Drawer button
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout_quiz);
        toggle = new ActionBarDrawerToggle(
                this, drawer,toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        //Setting Navigation Item Listeners
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view_quiz);
        navigationView.setNavigationItemSelectedListener(this);

    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout_quiz);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
    

}

更新2:

刚刚让我的操作栏变黑,并注意到第二个活动看不到整个操作栏。任何人都可以查看代码并找出错误:(

0 个答案:

没有答案