我有一个简单的导航抽屉应用程序,导航抽屉和汉堡包在我的主要活动中运行良好。
在";参观地点";activity我做的所有事情都和Main activity一样。我所要做的就是通过按下"键来打开NavigationDrawer;汉堡包";左上角的图标。";汉堡包";按钮是可见的,并且当我开始从左边拉动时,我看到按钮上的动画,但按下按钮并不能打开/关闭NavigationDrawer;参观场所活动";。
很抱歉造成任何混淆,以下是我目前正在尝试使用的简化代码:
这是我的主要活动代码:
package com.example.android.raidighitour;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import android.content.ClipData;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
private NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout =(DrawerLayout)findViewById(R.id.activity_main);
actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,R.string.open,R.string.close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
navigationView=(NavigationView)findViewById(R.id.navigation);
Intent i1= new Intent(this,VisitingPlace.class);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id= item.getItemId();
switch (id){
case R.id.home:
Toast.makeText(MainActivity.this,"THis is home",Toast.LENGTH_SHORT).show();
break;
case R.id.visiting_place:
startActivity(i1);
break;
default:
return true;
}
return true;
}
});
ImageView visiting_place=(ImageView)findViewById(R.id.image_visiting_place);
visiting_place.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i2= new Intent(MainActivity.this,VisitingPlace.class);
startActivity(i2);
}
});
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if(actionBarDrawerToggle.onOptionsItemSelected(item))return true;
return super.onOptionsItemSelected(item);
}
}
这是我的活动主。XMl
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@android:color/holo_green_dark"
tools:context=".MainActivity"
tools:openDrawer="start">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ViewFlipper
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoStart="true"
android:id="@+id/sliding_image"
android:flipInterval="3000">
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@drawable/jatardeul"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@drawable/raj_hotel"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@drawable/raidighi_college"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@drawable/raidighi_high_school"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@drawable/raidighi_rural_hospital"/>
</ViewFlipper>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/sliding_image"
android:id="@+id/first_row_circular_images"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="@drawable/circular"
android:src= "@drawable/visiting_place"
android:layout_marginTop="25dp"
android:layout_marginLeft="25dp"
android:id="@+id/image_visiting_place"/>
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="@drawable/circular"
android:src= "@drawable/emergency_help"
android:layout_marginTop="25dp"
android:layout_marginLeft="25dp"
android:layout_marginStart="25dp" />
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="@drawable/circular"
android:src= "@drawable/public_place"
android:layout_marginTop="25dp"
android:layout_marginLeft="25dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/first_row_circular_images"
android:id="@+id/second_row_circular_images"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="@drawable/circular"
android:src= "@drawable/exiting_point"
android:layout_marginTop="25dp"
android:layout_marginLeft="25dp"
android:layout_marginStart="25dp" />
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="@drawable/circular"
android:src= "@drawable/events"
android:layout_marginTop="25dp"
android:layout_marginLeft="25dp"
android:layout_marginStart="25dp" />
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="100dp"
android:background="@drawable/circular"
android:src= "@drawable/education"
android:layout_marginTop="25dp"
android:layout_marginLeft="25dp"
android:layout_marginStart="25dp" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/second_row_circular_images"
android:text="Hi this is a editable text. U can edit it."
android:textColor="@android:color/holo_purple"
android:textSize="49sp"
android:textStyle="bold"
android:layout_margin="60dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/navigation_menu"
app:headerLayout="@layout/nav_header"
android:fitsSystemWindows="true"
android:id="@+id/navigation"
android:layout_gravity="start">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
参观地点代码:
package com.example.android.raidighitour;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
public class VisitingPlace extends AppCompatActivity {
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
private NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_visiting_place);
drawerLayout =(DrawerLayout)findViewById(R.id.visiting_place);
actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,R.string.open,R.string.close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
navigationView=(NavigationView)findViewById(R.id.navigation);
Intent home_acitivity= new Intent(this,MainActivity.class);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id= item.getItemId();
switch (id){
case R.id.home:
startActivity(home_acitivity);
break;
case R.id.visiting_place:
break;
default:
return true;
}
return true;
}
});
}
}
Visiting_Place XML代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/visiting_place"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="@android:color/holo_green_dark"
tools:context=".VisitingPlace"
tools:openDrawer="start">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="45sp"
android:text="This is visiting place"
android:textColor="@color/design_default_color_primary_dark"/>
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/navigation_menu"
app:headerLayout="@layout/nav_header"
android:fitsSystemWindows="true"
android:id="@+id/navigation"
android:layout_gravity="start">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
您还需要在VisitingPlace
活动中实现onOptionsItemSelected
。你在执行这个吗?
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
case R.id.home:
startActivity(home_acitivity);
break;
case R.id.visiting_place:
//Does not do anything!
break;
default:
return true;
也许是不小心忘了写代码。
尝试使用android.R.id.home
而不是R.id.home
。