这是我的drawermenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="@+id/nav_item_one"
android:icon="@drawable/ic_share_black_24dp"
android:title="Rate the App" />
<item
android:id="@+id/nav_item_two"
android:icon="@drawable/ic_share_black_24dp"
android:title="Contact Us" />
</menu>
这是我的主要活动课程文件
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.transition.AutoTransition;
import android.transition.TransitionManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.ShareActionProvider;
import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.drawerlayout.widget.DrawerLayout;
import com.google.android.material.navigation.NavigationView;
public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
Toolbar toolbar;
ActionBarDrawerToggle actionBarDrawerToggle;
ConstraintLayout expandableView;
Button arrowBtn;
CardView cardView;
NavigationView NavView;
private ShareActionProvider mShareActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.sidemenu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case R.id.mShare:
Intent i = new Intent(
android.content.Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(
android.content.Intent.EXTRA_TEXT, "My new app https://play.google.com/store/search?q=TECHHUBINDIAN");
startActivity(Intent.createChooser(
i,
"Share Via"));
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpToolbar();
expandableView = findViewById(R.id.expandableView);
arrowBtn = findViewById(R.id.arrowBtn);
cardView = findViewById(R.id.cardView);
Button openhsc = findViewById(R.id.openhsc);
openhsc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openhsc1();
}
});
arrowBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (expandableView.getVisibility()==View.GONE){
TransitionManager.beginDelayedTransition(cardView, new AutoTransition());
expandableView.setVisibility(View.VISIBLE);
arrowBtn.setBackgroundResource(R.drawable.ic_arrow_drop_up_black_24dp);
} else {
TransitionManager.beginDelayedTransition(cardView, new AutoTransition());
expandableView.setVisibility(View.GONE);
arrowBtn.setBackgroundResource(R.drawable.ic_arrow_drop_down_black_24dp);
}
}
});
}
public void openhsc1() {
Intent intent = new Intent(this,Main2Activity.class);
startActivity(intent);
}
private void setUpToolbar()
{
drawerLayout = findViewById(R.id.drawerLayout);
NavView = findViewById(R.id.Navview) ;
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
NavView.setItemTextColor(ColorStateList.valueOf(Color.BLACK));
actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout, toolbar,R.string.app_name, R.string.app_name);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
}
}
这是我的主要活动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/drawerLayout"
android:background="@drawable/gradient"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:paddingBottom="0dp"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/app_name"
app:titleTextColor="#000000"
android:background="#008B8B"
app:theme="@style/AppTheme.AppBarOverlay"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
style="@style/CardView.Light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="20dp"
android:paddingBottom="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<!--android:layout_height="123dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="314dp"-->
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintTop_toTopOf="@+id/textView2">
<!-- android:id="@+id/cardView2"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="50dp"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="231dp">-->
<ImageView
android:id="@+id/imageView2"
android:layout_width="92dp"
android:layout_height="92dp"
android:layout_marginEnd="23dp"
android:layout_marginRight="23dp"
android:scaleType="centerCrop"
android:src="@drawable/twealth"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintEnd_toStartOf="@+id/textView2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/textView2" />
<TextView
android:id="@+id/textView"
style="@style/Widget.AppCompat.TextView"
android:layout_width="196dp"
android:layout_height="49dp"
android:text="HSC Board"
android:textColor="#000000"
android:textSize="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/textView2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.4" />
<TextView
android:id="@+id/textView2"
style="@style/Widget.AppCompat.TextView"
android:layout_width="0dp"
android:layout_height="46dp"
android:text="Maharashtra State Board"
android:textColor="#000000"
android:textSize="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/arrowBtn"
app:layout_constraintStart_toEndOf="@+id/imageView2" />
<Button
android:id="@+id/arrowBtn"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_marginTop="14dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="14dp"
android:background="@drawable/ic_arrow_drop_down_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toTopOf="parent" />
<!-- <LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/imageView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/good_morning" />
</LinearLayout>
</LinearLayout>-->
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/expandableView"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<!-- <TextView
android:id="@+id/textView3"
style="@style/Widget.AppCompat.TextView"
android:layout_width="356dp"
android:layout_height="97dp"
android:layout_marginStart="12dp"
android:layout_marginTop="109dp"
android:layout_marginEnd="12dp"
android:text=""
android:textColor="#000000"
android:textSize="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />-->
<Button
android:id="@+id/openhsc"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginHorizontal="80dp"
android:layout_marginBottom="20dp"
android:background="@drawable/rounded_button"
android:clickable="true"
android:focusable="true"
android:fontFamily="sans-serif-condensed-medium"
android:hapticFeedbackEnabled="true"
android:soundEffectsEnabled="true"
android:text="OPEN HSC PAPERS"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/space2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/space2" />
<Space
android:id="@+id/space2"
android:layout_width="366dp"
android:layout_height="40dp"
android:layout_marginTop="120dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/Navview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:headerLayout="@layout/header"
android:layout_gravity="start"
tools:menu="@menu/drawermenu"
>
</com.google.android.material.navigation.NavigationView>
<!-- <Button app:menu="@menu/drawermenu"
android:id="@+id/button1"
android:layout_width="336dp"
android:layout_height="0dp"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_marginTop="56dp"
android:layout_marginBottom="55dp"
android:background="@drawable/gradient"
android:clickable="true"
android:focusable="true"
android:fontFamily="sans-serif-condensed-medium"
android:hapticFeedbackEnabled="true"
android:soundEffectsEnabled="true"
android:text="@string/engineering"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/cardView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.493"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />-->
</androidx.drawerlayout.widget.DrawerLayout>
实际上,我认为此错误是由于我同时使用两个菜单而引起的,但我不知道实际发生了什么。
与此同时,我正在使用一个用于溢出菜单的菜单。该文件是sidemenu xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- <group android:checkableBehavior="single">-->
<item
android:id="@+id/mShare"
android:icon="@drawable/ic_share_black_24dp"
android:title="Share App"
app:showAsAction="always"
></item>
<!-- </group>-->
</menu>
对于NavigationView
,将tools:menu="@menu/drawermenu"
更改为app:menu="@menu/drawermenu"
。