缩小操作栏后,导航抽屉切换不起作用
Posted
技术标签:
【中文标题】缩小操作栏后,导航抽屉切换不起作用【英文标题】:After making actionbar smaller, navigation drawer toggle doesn't work 【发布时间】:2016-04-27 07:21:39 【问题描述】:我有一个带有切换功能的操作栏,但由于某种原因,每次我更改操作栏的颜色时,整个页面的颜色都会改变。我发现这是因为操作栏占据了整个屏幕。我对此进行了一些修复(将操作栏移动到更大的线性布局中,而不是将其保留在抽屉布局本身中),但是现在由于处理了这一点,切换不再起作用,这可能是相关的,但是当我将导航抽屉拖出,它会挂起一秒钟,我必须从那里拉出来。提前感谢您的帮助。
mainactivity.java
package me.paxana.alerta;
import android.app.Activity;
import android.graphics.Color;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.app.Fragment;
import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.support.v7.widget.Toolbar;
import com.parse.ParseUser;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
import me.paxana.alerta.adapter.SlidingMenuAdapter;
import me.paxana.alerta.fragment.Fragment1;
import me.paxana.alerta.fragment.Fragment2;
import me.paxana.alerta.fragment.Fragment3;
import me.paxana.alerta.model.ItemSlideMenu;
public class MainActivity extends AppCompatActivity
public static final String TAG = MainActivity.class.getSimpleName();
private List<ItemSlideMenu> listSliding;
private SlidingMenuAdapter adapter;
private ListView listViewSliding;
private DrawerLayout drawerLayout;
private android.support.v7.app.ActionBarDrawerToggle actionBarDrawerToggle;
private Toolbar mToolbar;
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
ParseUser currentUser = ParseUser.getCurrentUser();
//check to see if user is logged in
if (currentUser == null)
navigateToLogin(); //if not, send them to the login page
else //if they are, send the username to the log (debug only)
Log.i(TAG, currentUser.getUsername());
listViewSliding = (ListView)findViewById(R.id.lv_sliding_menu);
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
listSliding = new ArrayList<>();
//add item for sliding list
listSliding.add(new ItemSlideMenu(R.drawable.ic_action_settings, "Map"));
listSliding.add(new ItemSlideMenu(R.drawable.ic_action_about, "Emergency Call"));
listSliding.add(new ItemSlideMenu(R.drawable.ic_account_plus_black_48dp, "Friends"));
listSliding.add(new ItemSlideMenu(R.drawable.ic_logout_black_48dp, "Log Out"));
adapter = new SlidingMenuAdapter(this, listSliding);
listViewSliding.setAdapter(adapter);
//display icon to open/close slider
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mToolbar.setBackgroundColor(Color.DKGRAY);
mToolbar.setTitleTextColor(Color.BLACK);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//item selected
listViewSliding.setItemChecked(0, true);
//close menu
drawerLayout.closeDrawer(listViewSliding);
//handle on item click
//at some point it may make sense to change this to sort by item ID instead of position
listViewSliding.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
if (position == 3)
ParseUser.logOut();
navigateToLogin();
else
//replace fragment
replaceFragment(position);
//item selected
listViewSliding.setItemChecked(position, true);
//close menu
drawerLayout.closeDrawer(listViewSliding);
);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_opened, R.string.drawer_closed)
@Override
public void onDrawerOpened(View drawerView)
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
@Override
public void onDrawerClosed(View drawerView)
super.onDrawerClosed(drawerView);
invalidateOptionsMenu();
;
drawerLayout.setDrawerListener(actionBarDrawerToggle);
private void navigateToLogin()
Intent intent = new Intent(this, LoginActivity.class);
//these flags remove the mainactivity from the history, so you can't back your way into mainactivity from the login screen
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
@Override
protected void onPostCreate(Bundle savedInstanceState)
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
//create method replace fragment
private void replaceFragment(int position)
android.support.v4.app.Fragment fragment = null;
switch (position)
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
case 2:
fragment = new Fragment3();
break;
default:
fragment = new Fragment1();
break;
if(null != fragment)
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_content, fragment);
transaction.addToBackStack(null);
transaction.commit();
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_>
<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:orientation="vertical"
android:layout_
android:layout_
android:id="@+id/drawer_layout"
tools:context="me.paxana.alerta.MainActivity">
<RelativeLayout
android:layout_
android:layout_
android:id="@+id/main_content"
android:layout_alignParentTop="true">
</RelativeLayout>
<ListView
android:layout_
android:layout_
android:id="@+id/lv_sliding_menu"
android:background="#FFFFFF"
android:choiceMode="singleChoice"
android:layout_gravity="start"
android:paddingTop="?attr/actionBarSize"/>
</android.support.v4.widget.DrawerLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_
android:layout_
android:minHeight="?attr/actionBarSize"/>
</FrameLayout>
【问题讨论】:
【参考方案1】:我用谷歌搜索了迈克的答案并找到了我需要做的事情。我加了
@Override
public boolean onOptionsItemSelected(MenuItem item)
if(actionBarDrawerToggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
当我摆脱溢出菜单时,我已经删除了这段代码。
【讨论】:
不用抱歉,非常感谢您的帮助!如果您以后想提出答案,我会给您最好的答案以上是关于缩小操作栏后,导航抽屉切换不起作用的主要内容,如果未能解决你的问题,请参考以下文章