带有AppCompatActivity的ActionBarDrawerToggle

时间:2017-05-26 16:00:15

标签: android

我正在尝试使用以下代码添加ActionBarDrawerToggle:

package com.i3techs.mtricks;

import android.app.SearchManager;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.ActivityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.i3techs.mtricks.common.SharedPrefManager;
import com.i3techs.mtricks.event.SuccessLoginEvent;
import com.i3techs.mtricks.event.UserErrorEvent;
import com.i3techs.mtricks.model.entity.User;
import com.i3techs.mtricks.presenter.UserPresenter;
import com.i3techs.mtricks.presenter.UserPresenterImpl;
import com.i3techs.mtricks.view.UserView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class MainActivity extends AppCompatActivity  {
    @BindView(R.id.layoutDrawer) DrawerLayout layoutDrawer;
    @BindView(R.id.left_drawer) ListView drawerLeft;
    ActionBarDrawerToggle barDrawerToggle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initializeElements();

    }

    private void initializeElements()
    {
        String[] navMenu = getResources().getStringArray(R.array.left_nav_menu);
        drawerLeft.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, navMenu));
        barDrawerToggle= new ActionBarDrawerToggle(this,layoutDrawer,R.string.open,R.string.close){

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
               // getActionBar().setTitle("gu");
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
             //   getActionBar().setTitle("fgcxgf");
            }
        };
        // Set the drawer toggle as the DrawerListener
        layoutDrawer.setDrawerListener(barDrawerToggle);


    }


    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.options_menu, menu);
        // Associate searchable configuration with the SearchView
        SearchManager searchManager =
                (SearchManager) getSystemService(getApplicationContext().SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.search).getActionView();

        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));
        return true;
    }
}

我的XML文件是:

<!-- Main content view -->

<fragment
    android:layout_width="match_parent"
    android:id="@+id/fragmentTrick"
    class="com.i3techs.mtricks.TricksFragment"
    android:layout_height="match_parent">
</fragment>

Still Toggle没有出现在操作栏中。我使用以下风格:

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

0 个答案:

没有答案