如何向现有应用程序添加导航抽屉?
Posted
技术标签:
【中文标题】如何向现有应用程序添加导航抽屉?【英文标题】:How can i add a navigation drawer to an existing application? 【发布时间】:2021-06-17 17:37:23 【问题描述】:我是制作应用程序的新手,我正在从事这个需要帮助的大学项目,我想为我的应用程序创建一个导航抽屉,但我不知道如何,我已经创建了登录,在我的应用程序中注册、编辑个人资料和主页,但现在我想在不创建全新项目的情况下为其添加导航抽屉,例如,我希望主页具有该抽屉,它将包含“编辑个人资料”等我可以从中进行不同的活动,我看过很多视频但还不太了解,因为它们都在制作新项目,我如何将我的所有活动链接到它而不需要创建新项目? main_activity 代码
package net.authenticatorapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.squareup.picasso.Picasso;
import javax.annotation.Nullable;
public class MainActivity extends AppCompatActivity
private static final int GALLERY_INTENT_CODE = 1023 ;
TextView fullName,email,phone,verifyMsg;
FirebaseAuth fAuth;
FirebaseFirestore fStore;
String userId;
Button resendCode;
Button resetPassLocal,changeProfileImage;
FirebaseUser user;
ImageView profileImage;
StorageReference storageReference;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.nav_activity_main);
phone = findViewById(R.id.profilePhone);
fullName = findViewById(R.id.profileName);
email = findViewById(R.id.profileEmail);
resetPassLocal = findViewById(R.id.resetPasswordLocal);
profileImage = findViewById(R.id.profileImage);
changeProfileImage = findViewById(R.id.changeProfile);
fAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();
storageReference = FirebaseStorage.getInstance().getReference();
StorageReference profileRef = storageReference.child("users/"+fAuth.getCurrentUser().getUid()+"/profile.jpg");
profileRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>()
@Override
public void onSuccess(Uri uri)
Picasso.get().load(uri).into(profileImage);
);
resendCode = findViewById(R.id.resendCode);
verifyMsg = findViewById(R.id.verifyMsg);
userId = fAuth.getCurrentUser().getUid();
user = fAuth.getCurrentUser();
if(!user.isEmailVerified())
verifyMsg.setVisibility(View.VISIBLE);
resendCode.setVisibility(View.VISIBLE);
resendCode.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(final View v)
user.sendEmailVerification().addOnSuccessListener(new OnSuccessListener<Void>()
@Override
public void onSuccess(Void aVoid)
Toast.makeText(v.getContext(), "Verification Email Has been Sent.", Toast.LENGTH_SHORT).show();
).addOnFailureListener(new OnFailureListener()
@Override
public void onFailure(@NonNull Exception e)
Log.d("tag", "onFailure: Email not sent " + e.getMessage());
);
);
DocumentReference documentReference = fStore.collection("users").document(userId);
documentReference.addSnapshotListener(this, new EventListener<DocumentSnapshot>()
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e)
if(documentSnapshot.exists())
phone.setText(documentSnapshot.getString("phone"));
fullName.setText(documentSnapshot.getString("fName"));
email.setText(documentSnapshot.getString("email"));
else
Log.d("tag", "onEvent: Document do not exists");
);
resetPassLocal.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
final EditText resetPassword = new EditText(v.getContext());
final AlertDialog.Builder passwordResetDialog = new AlertDialog.Builder(v.getContext());
passwordResetDialog.setTitle("Reset Password ?");
passwordResetDialog.setMessage("Enter New Password > 8 Characters long.");
passwordResetDialog.setView(resetPassword);
passwordResetDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
// extract the email and send reset link
String newPassword = resetPassword.getText().toString();
user.updatePassword(newPassword).addOnSuccessListener(new OnSuccessListener<Void>()
@Override
public void onSuccess(Void aVoid)
Toast.makeText(MainActivity.this, "Password Reset Successfully.", Toast.LENGTH_SHORT).show();
).addOnFailureListener(new OnFailureListener()
@Override
public void onFailure(@NonNull Exception e)
Toast.makeText(MainActivity.this, "Password Reset Failed.", Toast.LENGTH_SHORT).show();
);
);
passwordResetDialog.setNegativeButton("No", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
// close
);
passwordResetDialog.create().show();
);
changeProfileImage.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
// open gallery
Intent i = new Intent(v.getContext(),EditProfile.class);
i.putExtra("fullName",fullName.getText().toString());
i.putExtra("email",email.getText().toString());
i.putExtra("phone",phone.getText().toString());
startActivity(i);
//
);
public void logout(View view)
FirebaseAuth.getInstance().signOut();//logout
startActivity(new Intent(getApplicationContext(),Login.class));
finish();
main_activity 布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_
android:layout_
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:onClick="logout"
android:text="Logout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/resetPasswordLocal" />
<ImageView
android:id="@+id/profileImage"
android:layout_
android:layout_
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.13999999"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/profileName"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:text="Your Name"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView2"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<TextView
android:id="@+id/profileEmail"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:text="Your Email Address"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView3"
app:layout_constraintTop_toBottomOf="@+id/textView4" />
<TextView
android:id="@+id/profilePhone"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Phone Number"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView4"
app:layout_constraintTop_toBottomOf="@+id/textView5" />
<TextView
android:id="@+id/verifyMsg"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Email Not Verfied !"
android:textColor="#C2185B"
android:textSize="20sp"
android:textStyle="bold"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/resendCode"
android:layout_
android:layout_
android:text="Verify Now"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/verifyMsg" />
<Button
android:id="@+id/resetPasswordLocal"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="Reset Password"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/changeProfile"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="@+id/textView3"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginEnd="16dp"
android:text="Full Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView2"
app:layout_constraintTop_toTopOf="@+id/imageView2" />
<TextView
android:id="@+id/textView4"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginEnd="16dp"
android:text="Email"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView3"
app:layout_constraintTop_toTopOf="@+id/imageView3" />
<ImageView
android:id="@+id/imageView2"
android:layout_
android:layout_
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/profileImage"
app:layout_constraintVertical_bias="0.050000012"
app:srcCompat="@drawable/ic_person_black_24dp" />
<ImageView
android:id="@+id/imageView3"
android:layout_
android:layout_
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/profileName"
app:srcCompat="@drawable/ic_email_black_24dp" />
<ImageView
android:id="@+id/imageView4"
android:layout_
android:layout_
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/profileEmail"
app:srcCompat="@drawable/ic_local_phone_black_24dp" />
<TextView
android:id="@+id/textView5"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginEnd="16dp"
android:text="Phone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView4"
app:layout_constraintTop_toTopOf="@+id/imageView4" />
<Button
android:id="@+id/changeProfile"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="Change Profile"
app:layout_constraintBottom_toBottomOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/profilePhone"
app:layout_constraintVertical_bias="0.59000003" />
<TextView
android:id="@+id/textView"
android:layout_
android:layout_
android:text="Profile"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.107"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.043" />
</androidx.constraintlayout.widget.ConstraintLayout>
【问题讨论】:
导航 UI 可以让您的生活变得轻松。 @Eyosiyas 我不知道该怎么做,我应该做一个全新的项目吗? developer.android.com/guide/navigation/navigation-ui @HanaaAlaouiBelghiti 您不必创建新项目。 @Eyosiyas 那我该怎么做呢?我看过很多视频,我真的不知道 【参考方案1】:您必须进行一些更改。 变化如下。 首先,我们需要修改main_activity.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:layout_
android:id="@+id/drawer"
android:layout_
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
<Button
android:id="@+id/button"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:onClick="logout"
android:text="Logout"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/resetPasswordLocal" />
<ImageView
android:id="@+id/profileImage"
android:layout_
android:layout_
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.13999999"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/profileName"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:text="Your Name"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView2"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
<TextView
android:id="@+id/profileEmail"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:text="Your Email Address"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView3"
app:layout_constraintTop_toBottomOf="@+id/textView4" />
<TextView
android:id="@+id/profilePhone"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Phone Number"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView4"
app:layout_constraintTop_toBottomOf="@+id/textView5" />
<TextView
android:id="@+id/verifyMsg"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Email Not Verfied !"
android:textColor="#C2185B"
android:textSize="20sp"
android:textStyle="bold"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/resendCode"
android:layout_
android:layout_
android:text="Verify Now"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/verifyMsg" />
<Button
android:id="@+id/resetPasswordLocal"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="Reset Password"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/changeProfile"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="@+id/textView3"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginEnd="16dp"
android:text="Full Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView2"
app:layout_constraintTop_toTopOf="@+id/imageView2" />
<TextView
android:id="@+id/textView4"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginEnd="16dp"
android:text="Email"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView3"
app:layout_constraintTop_toTopOf="@+id/imageView3" />
<ImageView
android:id="@+id/imageView2"
android:layout_
android:layout_
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/profileImage"
app:layout_constraintVertical_bias="0.050000012"
app:srcCompat="@drawable/ic_person_black_24dp" />
<ImageView
android:id="@+id/imageView3"
android:layout_
android:layout_
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/profileName"
app:srcCompat="@drawable/ic_email_black_24dp" />
<ImageView
android:id="@+id/imageView4"
android:layout_
android:layout_
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/profileEmail"
app:srcCompat="@drawable/ic_local_phone_black_24dp" />
<TextView
android:id="@+id/textView5"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginEnd="16dp"
android:text="Phone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView4"
app:layout_constraintTop_toTopOf="@+id/imageView4" />
<Button
android:id="@+id/changeProfile"
android:layout_
android:layout_
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="Change Profile"
app:layout_constraintBottom_toBottomOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/profilePhone"
app:layout_constraintVertical_bias="0.59000003" />
<TextView
android:id="@+id/textView"
android:layout_
android:layout_
android:text="Profile"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.107"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.043" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigationView"
android:layout_
android:layout_
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/nav_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
接下来,我们将在 layout 资源目录中为 Navigation Drawer Header 布局创建 nav_header.xml 布局文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_
android:layout_
android:gravity="bottom"
android:orientation="vertical"
android:padding="8dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<TextView
android:layout_
android:layout_
android:layout_gravity="center"
android:paddingTop="8dp"
android:text="Nav Drawer Text"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="@android:color/black" />
</LinearLayout>
我们还需要在menu资源目录下创建一个菜单。
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_call"
android:title="Call" />
<item
android:id="@+id/nav_message"
android:title="Message" />
<item
android:id="@+id/nav_history"
android:title="History" />
<item
android:id="@+id/nav_view"
android:title="View contact" />
<item
android:id="@+id/nav_edit"
android:title="Edit contact" />
<item
android:id="@+id/nav_logout"
android:title="Log out" />
</menu>
转到 MainActivity.java 并添加以下内容。
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.nav_activity_main);
phone = findViewById(R.id.profilePhone);
fullName = findViewById(R.id.profileName);
email = findViewById(R.id.profileEmail);
resetPassLocal = findViewById(R.id.resetPasswordLocal);
//You need a toolbar here. Create your own or use the Theme's one.
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer);
NavigationView navigationView = findViewById(R.id.navigationView);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
@Override
public boolean onNavigationItemSelected(MenuItem item)
if(item.getItemId() == R.id.nav_call)
Toast.makeText(getApplicationContext(),"Hello",Toast.LENGTH_SHORT).show();
//Do this for the rest of the navigation menu items.
return true;
);
转到您的 strings.xml 资源文件并添加以下内容
<string name="navigation_drawer_open">Drawer Open</string>
<string name="navigation_drawer_close">Drawer Closed</string>
我想我们已经完成了。
【讨论】:
以上是关于如何向现有应用程序添加导航抽屉?的主要内容,如果未能解决你的问题,请参考以下文章
在Recycler视图上添加图像视图时,Android导航抽屉动画滞后/减速