我正在尝试为多种活动的自行车开发应用程序。 我开发了一个带有多个活动和工具栏而不是操作栏的导航抽屉。在主要活动(TimerActivity)中,工具栏可以工作,但是当我点击icon_drawer时它不适用于其他活动(它只适用于滑动)。我该如何解决这个问题? 似乎工具栏被上面的另一个布局覆盖。 抱歉英文不好。
TimerActivity.java
public class TimerActivity extends ActionBarActivity {
private static String CLASS_NAME;
private Vibrator vibrate;
private Notify notify;
protected Toolbar toolbar;
protected DrawerLayout drawerLayout;
protected ActionBarDrawerToggle drawerToggle;
public ListView leftDrawerList;
protected ArrayList<navDrawerItems> NavDrawerItems;
protected NavDrawerListAdapter adapter;
private TypedArray navMenuIcons;
protected String[] navMenuTitles;
private CharSequence mTitle;
private LinearLayout linearLayout;
protected FrameLayout frameLayout;
public TimerActivity() {
CLASS_NAME = getClass().getName();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(CLASS_NAME, "onCreate");
setContentView(R.layout.activity_main);
frameLayout = (FrameLayout) findViewById(R.id.frame_container);
initView();
toolbar.setTitle("Navigation Drawer");
setSupportActionBar(toolbar);
initDrawer();
leftDrawerList.setOnItemClickListener(new SlideMenuClickListener());
}
protected void initView() {
//load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
//nav drawer icons from resources
navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
leftDrawerList = (ListView) findViewById(R.id.left_drawer);
toolbar = (Toolbar) findViewById(R.id.toolbar);
NavDrawerItems = new ArrayList<navDrawerItems>();
//adding nav drawer items to array
//Home
NavDrawerItems.add(new navDrawerItems(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
//User
NavDrawerItems.add(new navDrawerItems(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
//Map
NavDrawerItems.add(new navDrawerItems(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
//Routes
NavDrawerItems.add(new navDrawerItems(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
//Camera
NavDrawerItems.add(new navDrawerItems(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
//Weather
NavDrawerItems.add(new navDrawerItems(navMenuTitles[5], navMenuIcons.getResourceId(5, -1)));
//Recycle the typed array
navMenuIcons.recycle();
//setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),NavDrawerItems);
leftDrawerList.setAdapter(adapter);
}
protected void initDrawer() {
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
drawerLayout.setDrawerListener(drawerToggle);
}
private void gotoRoutes() {
Log.d(CLASS_NAME, "gotoRoutes");
Intent routes = new Intent(this, RoutesActivity.class);
startActivity(routes);
}
/**
* Called when the settings button is clicked on.
*
* @param view
* the button clicked on
*/
public void gotoSettings(View view) {
Log.d(CLASS_NAME, "gotoSettings");
Intent settings = new Intent(this, SettingsActivity.class);
startActivity(settings);
}
private void gotoHome() {
Log.d(CLASS_NAME, "gotoHome");
Intent timer = new Intent(this, TimerActivity.class);
timer.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(timer);
}
public void gotoMap() {
Log.d(CLASS_NAME, "gotoMap");
Intent map = new Intent(this, MapActivity.class);
startActivity(map);
}
public void gotoPhoto() {
Log.d(CLASS_NAME, "gotoPhoto");
Intent photo = new Intent(this, PhotoActivity.class);
startActivity(photo);
}
public void shareActivity() {
Log.d(CLASS_NAME, "shareActivity");
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case android.R.id.home:
gotoHome();
return true;
case R.id.menu_settings:
gotoSettings(null);
return true;
case R.id.menu_routes:
gotoRoutes();
return true;
case R.id.menu_map:
gotoMap();
return true;
case R.id.menu_photo:
gotoPhoto();
return true;
case R.id.menu_share:
shareActivity();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void notification(String title, String message) {
notify.notify(title, message);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(CLASS_NAME, "onDestroy");
}
@Override
public void onRestart() {
super.onRestart();
Log.d(CLASS_NAME, "onRestart");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.d(CLASS_NAME, "onCreateOptionsMenu");
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
/**
* Keep the screen on depending on the stay awake setting
*/
protected void stayAwakeOrNot() {
Log.d(CLASS_NAME, "stayAwakeOrNot");
Settings settings = ((OnYourBike) getApplication()).getSettings();
if (settings.isCaffeinated(this)) {
// Log.i(CLASS_NAME, "Staying awake");
getWindow()
.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} else {
// Log.i(CLASS_NAME, "Not staying awake");
getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
public void takePhoto(View view){
Log.d(CLASS_NAME, "takePhoto");
//if(camera.hasCamera() && camera.hasCameraApplication()){
// camera.takePhoto();
Intent photo = new Intent(this, PhotoActivity.class);
startActivity(photo);
}
public void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch(position){
case 0:
Intent intent1 = new Intent(this, CounterActivity.class);
startActivity(intent1);
finish();
break;
case 1:
Intent intent2 = new Intent(this, UserActivity.class);
startActivity(intent2);
finish();
break;
case 2:
Intent intent3 = new Intent(this, MapActivity.class);
startActivity(intent3);
finish();
break;
case 3:
Intent intent4 = new Intent(this, RoutesActivity.class);
startActivity(intent4);
finish();
break;
case 4:
Intent intent5 = new Intent(this, PhotoActivity.class);
startActivity(intent5);
finish();
break;
case 5:
Intent intent6 = new Intent(this, FragmentWeather.class);
startActivity(intent6);
finish();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
leftDrawerList.setItemChecked(position,true);
leftDrawerList.setSelection(position);
//setTitle(navMenuTitles[position]);
//drawerLayout.closeDrawer(leftDrawerList);
drawerLayout.closeDrawer(leftDrawerList);
toolbar.setTitle(navMenuTitles[position]);
}
else{
// error in creating fragment
Log.e("TimerActivity", "Error in creating fragment");
}
}
class SlideMenuClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
displayView(position);
}
}
}
activity_main.xml(TimerActivity的布局)
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="7dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar" />
</LinearLayout>
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- navigation drawer -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#A0E843"
android:divider="#eee"
android:dividerHeight="1dp" />
</android.support.v4.widget.DrawerLayout>
UserActivity.java
public class UserActivity extends TimerActivity {
private int position;
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_user);
getLayoutInflater().inflate(R.layout.activity_user, frameLayout );
leftDrawerList.setItemChecked(position,true);
toolbar = (Toolbar) findViewById(R.id.toolbar);
//frameLayout = (FrameLayout) findViewById(R.id.frame_container);
toolbar.setTitle("Useeeer activity");
setSupportActionBar(toolbar);
drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
setSupportActionBar(toolbar);
super.initView();
super.initDrawer();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.user, menu);
return true;
}
}
user_activity.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="7dp" >
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/toolbar" />
</FrameLayout>
<!-- Framelayout to display Fragments -->
<!-- navigation drawer -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#A0E843"
android:divider="#eee"
android:dividerHeight="1dp" />
</android.support.v4.widget.DrawerLayout>
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
答案 0 :(得分:0)
我从您的代码中看到的是以这种方式使用工具栏的工作活动:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar" />
</LinearLayout>
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
这是真的! 但是其他布局不遵循这种结构,它是这样的:
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/toolbar" />
</FrameLayout>
尝试修复此问题并告诉我它是否有效!
答案 1 :(得分:0)
之前我遇到过这样的问题。您的活动可能会将焦点从工具栏中移开。
在包含工具栏的Activity XML中,尝试将这些添加到.xml文件中的<Toolbar>
项目中:
android:descendantFocusability="false
android:focusable="false"
答案 2 :(得分:0)
尝试更改activity_main.xml - 将工具栏移到DrawerLayout外部:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar" />
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="7dp" >
</LinearLayout>
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- navigation drawer -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#A0E843"
android:divider="#eee"
android:dividerHeight="1dp" />
</android.support.v4.widget.DrawerLayout>