所以我正在制作一个移动应用程序,在我的主菜单中,在多次尝试隐藏它之后,操作栏/标题栏仍然显示。
我尝试更改主题,但不起作用,我尝试将其隐藏在 homefragment 代码中,并在 ProfileFragment 中执行相同的操作,我确实尝试在托管片段的活动中解决它,但仍然一无所获。我有点迷失所以我需要帮助解决这个问题。
首页片段:
package com.example.ambicheck.ui.home;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.ambicheck.CreateRoomActivity;
import com.example.ambicheck.DataClass;
import com.example.ambicheck.MyAdapter;
import com.example.ambicheck.R;
import com.example.ambicheck.databinding.FragmentHomeBinding;
import com.example.ambicheck.recyclerViewMain;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
public class HomeFragment extends Fragment {
private FragmentHomeBinding binding;
private TextView todasTextView;
private TextView criarDivisaoTextView;
RecyclerView recyclerView;
List<DataClass> dataList;
MyAdapter adapter;
DataClass androidData;
TextView olaTextView;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
if (getActivity() != null && getActivity().getActionBar() != null) {
getActivity().getActionBar().hide();
}
binding = FragmentHomeBinding.inflate(inflater, container, false);
View root = binding.getRoot();
todasTextView = root.findViewById(R.id.todas);
olaTextView = root.findViewById(R.id.Ola);
criarDivisaoTextView = root.findViewById(R.id.criarDivisao);
todasTextView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(), recyclerViewMain.class));
}
});
criarDivisaoTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), CreateRoomActivity.class);
startActivity(intent);
}
});
recyclerView = root.findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
dataList = new ArrayList<>();
adapter = new MyAdapter(getActivity(), dataList, R.layout.recycler_item);
recyclerView.setAdapter(adapter);
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
String name = user.getDisplayName();
String email = user.getEmail();
olaTextView.setText("Olá, " + name + " 👋");
}
DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference("Rooms");
dbRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
String userId = user.getUid();
dataList.clear();
for (DataSnapshot roomSnapshot : dataSnapshot.getChildren()) {
String id = roomSnapshot.getKey(); // Get the room's ID from the snapshot
String roomName = roomSnapshot.child("roomName").getValue(String.class); // Get the room's name
Integer ratingValue = roomSnapshot.child("rating").getValue(Integer.class); // Get the room's rating
int rating = ratingValue != null ? ratingValue : 0; // If the rating is null, default to 0
String description = roomSnapshot.child("description").getValue(String.class); // Get the room's description
String imageUrl = roomSnapshot.child("imageUrl").getValue(String.class); // Get the room's image URL
String roomUserId = roomSnapshot.child("userId").getValue(String.class); // Get the user ID associated with the room
if (userId.equals(roomUserId)) { // Only add the room to the dataList if the user ID matches
DataClass roomData = new DataClass(id, roomName, rating, description, imageUrl,userId); // Create a new DataClass object with the retrieved data
dataList.add(roomData); // Add the new DataClass object to the dataList
}
}
// Notify the adapter of the change
adapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
// Trate o erro
}
});
return root;
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
个人资料片段:
package com.example.ambicheck.ui.perfil;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import com.bumptech.glide.Glide;
import com.example.ambicheck.Activity_Definicoes;
import com.example.ambicheck.Activity_GestaoF;
import com.example.ambicheck.Activity_Login;
import com.example.ambicheck.Activity_Perfil;
import com.example.ambicheck.Activity_ServicoCloud;
import com.example.ambicheck.R;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import io.getstream.avatarview.AvatarView;
public class PerfilFragment extends Fragment {
private static final int PICK_IMAGE = 1;
private AvatarView avatarView;
private Button btnLogout;
private Button definicoesButton;
private Button gestaoFamiliasButton;
private TextView nomeUtilizador;
private TextView emailTextview;
private ImageView profileImageView;
private FirebaseAuth mAuth;
private Uri selectedImageUri;
private Context mContext;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_perfil, container, false);
mContext = getActivity();
mAuth = FirebaseAuth.getInstance();
btnLogout = view.findViewById(R.id.buttonLogout);
definicoesButton = view.findViewById(R.id.definicoes);
gestaoFamiliasButton = view.findViewById(R.id.GestaoFamilias);
avatarView = view.findViewById(R.id.avatar_view);
nomeUtilizador = view.findViewById(R.id.nomeUtilizador);
profileImageView = view.findViewById(R.id.profileImageView);
emailTextview = view.findViewById(R.id.emailTextview);
// Fetch and display profile image
fetchAndDisplayProfileImage();
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
if (currentUser != null) {
String userName = currentUser.getDisplayName();
String userEmail = currentUser.getEmail();
nomeUtilizador.setText(userName);
emailTextview.setText(userEmail);
}
if (getActivity() != null && getActivity().getActionBar() != null) {
getActivity().getActionBar().hide();
}
profileImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, PICK_IMAGE);
}
});
btnLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAuth.signOut();
Intent intent = new Intent(getActivity(), Activity_Login.class);
startActivity(intent);
}
});
definicoesButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(), Activity_Definicoes.class));
}
});
gestaoFamiliasButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(), Activity_GestaoF.class));
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == getActivity().RESULT_OK && data != null) {
selectedImageUri = data.getData();
uploadImageToFirebaseStorage();
}
}
private void uploadImageToFirebaseStorage() {
if (selectedImageUri != null) {
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReference();
StorageReference profileImagesRef = storageRef.child("profile_images/" + FirebaseAuth.getInstance().getCurrentUser().getUid() + ".jpg");
profileImagesRef.putFile(selectedImageUri)
.addOnSuccessListener(taskSnapshot -> {
profileImagesRef.getDownloadUrl().addOnSuccessListener(uri -> {
String imageURL = uri.toString();
saveImageURLToDatabase(imageURL);
});
})
.addOnFailureListener(e -> {
Toast.makeText(mContext, "Failed to upload image to Firebase Storage", Toast.LENGTH_SHORT).show();
});
}
}
private void saveImageURLToDatabase(String imageURL) {
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
if (currentUser != null) {
String userId = currentUser.getUid();
DatabaseReference userRef = FirebaseDatabase.getInstance().getReference("Users").child(userId);
userRef.child("imagemPerfil").setValue(imageURL)
.addOnSuccessListener(aVoid -> {
Glide.with(mContext).load(imageURL).into(profileImageView);
})
.addOnFailureListener(e -> {
Toast.makeText(mContext, "Failed to save image URL to database", Toast.LENGTH_SHORT).show();
});
}
}
private void fetchAndDisplayProfileImage() {
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
if (currentUser != null) {
String userId = currentUser.getUid();
DatabaseReference userRef = FirebaseDatabase.getInstance().getReference("Users").child(userId);
userRef.child("imagemPerfil").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String imageURL = dataSnapshot.getValue(String.class);
if (imageURL != null && !imageURL.isEmpty()) {
Glide.with(mContext).load(imageURL).into(profileImageView);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
// Handle error
}
});
}
}
}
主题:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.noActionBar.Ambicheck" parent="Theme.Material3.DayNight.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.noActionBar.Ambicheck"
tools:targetApi="31">
<activity
android:name=".GroupDetailActivity"
android:exported="false" />
<activity
android:name=".CreateRoomActivity"
android:exported="false" />
<activity
android:name=".Activity_Notifications"
android:exported="false" />
<activity
android:name=".GroupMembersActivity"
android:exported="false" /> <!-- File: AndroidManifest.xml -->
<activity
android:name=".QRCodeScannerActivity"
android:exported="false" />
<activity
android:name=".Activity_CreacaoGrupos"
android:exported="false" />
<activity
android:name=".Activity_QRcode"
android:exported="false" />
<activity
android:name=".Activity_PerfilUser"
android:exported="false" />
<activity
android:name=".Activity_ServicoCloud"
android:exported="false" />
<activity
android:name=".Activity_GestaoF"
android:exported="false" />
<activity
android:name=".Activity_Definicoes"
android:exported="false" />
<activity
android:name=".Activity_notificacoes"
android:exported="false" />
<activity
android:name=".Activity_Perfil"
android:exported="false" />
<activity
android:name=".Activity_intro"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".recyclerViewMain" />
<activity android:name=".DetailActivity" />
<activity
android:name=".Activity_menu"
android:exported="false"
android:label="@string/title_activity_menu"
tools:ignore="DuplicateActivity" />
<activity
android:name=".Actitvity_RecuperarConta"
android:exported="false" />
<activity
android:name=".Activity_Registrar"
android:exported="false" />
<activity
android:name=".Activity_Login"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
活动菜单:
package com.example.ambicheck;
import android.os.Bundle;
import com.example.ambicheck.databinding.ActivityMenuBinding;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
public class Activity_menu extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
ActivityMenuBinding binding = ActivityMenuBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
// Find the NavHostFragment
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment_activity_menu2);
// Get the NavController
assert navHostFragment != null;
NavController navController = navHostFragment.getNavController();
// Find the BottomNavigationView
BottomNavigationView navView = findViewById(R.id.nav_view);
// Setup navigation with NavController
NavigationUI.setupWithNavController(navView, navController);
}
}```
使用 getSupportActionBar() 代替 getActionBar()。当您使用 AppCompatActivity 扩展您的 Activity 时,这是必需的。