【问题标题】:Position of tollbar is not set on the top and it is hiding behind other content工具栏的位置未设置在顶部,它隐藏在其他内容后面
【发布时间】:2021-04-21 08:36:58
【问题描述】:

我有工具栏,我想显示两个菜单图标,即 myicon_clearnotifications 和 /myicon_goback 但是当我单击时,只有通知不会在工具栏上显示这两个菜单项。看起来我的工具栏在通知后面(我希望通知显示在我的工具栏下方)其他内容正在显示但工具栏没有。

XML 工具栏代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite"
    tools:context="Activities.AllNotifications">
    
    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/allNotifications_tollbar"
        android:layout_alignParentTop="true"
        android:background="#5A6E64"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

        />


    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:id="@+id/allNotifications_RecyclerView"/>


</RelativeLayout>

我的菜单项代码

<?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">


    <item
        android:id="@+id/allNotifications_item_clear"
        android:title="CLEAR"
        android:icon="@drawable/myicon_clearnotifications"
        android:checkable="true"
        app:showAsAction="always"
        />

    <item
        android:id="@+id/allNotifications_item_goBack"
        android:title="GO BACK"
        android:icon="@drawable/myicon_goback"
        android:checkable="true"
        app:showAsAction="always"
        />
</menu>

这是我的java代码

package Activities;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;


import com.example.connectsocialmediaapp.R;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import com.google.firebase.firestore.WriteBatch;

import java.util.ArrayList;

import AdapterClasses.AllNotificationsAdapter;
import ModelClasses.Model_AllNotifications;

public class AllNotifications extends AppCompatActivity {

    private FirebaseFirestore objectFirebaseFirestore;
    private RecyclerView objectRecyclerView;

    private AllNotificationsAdapter objectAllNotificationsAdapter;
    private Toolbar objectToolbar;

    private FirebaseAuth objectFirebaseAuth;
    private Dialog objectDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_notifications);

        objectFirebaseFirestore=FirebaseFirestore.getInstance();
        objectFirebaseAuth=FirebaseAuth.getInstance();

        attachJavaToXML();
        getAllNotificationIntoRV();
    }

    @Override
    protected void onStart() {
        super.onStart();
        objectAllNotificationsAdapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        objectAllNotificationsAdapter.stopListening();
    }

    private void getAllNotificationIntoRV()
    {
        try
        {
            if(objectFirebaseAuth!=null) {
                String currentLoggedInUser = objectFirebaseAuth.getCurrentUser().getEmail();
                Query objectQuery = objectFirebaseFirestore.collection("userProfileData")
                        .document(currentLoggedInUser).collection("Notifications");

                FirestoreRecyclerOptions<Model_AllNotifications> objectOptions =
                        new FirestoreRecyclerOptions.Builder<Model_AllNotifications>()
                                .setQuery(objectQuery, Model_AllNotifications.class).build();
                objectAllNotificationsAdapter = new AllNotificationsAdapter(objectOptions);
                objectRecyclerView.setAdapter(objectAllNotificationsAdapter);

                objectRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            }
                else
                {
                    Toast.makeText(this, R.string.no_user_online, Toast.LENGTH_SHORT).show();
                }

        }
        catch (Exception e)
        {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    ArrayList<String> objectStringArrayList=new ArrayList<>();
    private void clearAllNotifications()
    {
        try
        {
            if(objectFirebaseAuth!=null)
            {
                final String currentLoggedInUser=objectFirebaseAuth.getCurrentUser().getEmail();
                objectFirebaseFirestore.collection("userProfileData")
                        .document(currentLoggedInUser)
                        .collection("Notifications")
                        .get()
                        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                            @Override
                            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                if(task.isSuccessful())
                                {
                                    for(QueryDocumentSnapshot objectQueryDocumentSnapshot:task.getResult())
                                    {
                                        objectStringArrayList.add(objectQueryDocumentSnapshot.getId());
                                        WriteBatch objectWriteBatch=objectFirebaseFirestore.batch();

                                        for(int count=0;count<objectStringArrayList.size();count++)
                                        {
                                            objectWriteBatch.delete(
                                                    objectFirebaseFirestore.collection("userProfileData")
                                                    .document(currentLoggedInUser)
                                                    .collection("Notifications")
                                                    .document(objectStringArrayList.get(count))
                                            );
                                        }
                                        objectWriteBatch.commit().addOnCompleteListener(new OnCompleteListener<Void>() {
                                            @Override
                                            public void onComplete(@NonNull Task<Void> task) {
                                                if(task.isSuccessful())
                                                {
                                                    Toast.makeText(AllNotifications.this, "Notifications Cleared", Toast.LENGTH_SHORT).show();
                                                }
                                                else if(!task.isSuccessful())
                                                {
                                                    Toast.makeText(AllNotifications.this, task.getException().toString(), Toast.LENGTH_SHORT).show();
                                                }
                                            }
                                        });
                                    }
                                }
                            }
                        });
            }
            else
            {
                Toast.makeText(this, R.string.no_user_online, Toast.LENGTH_SHORT).show();
            }
        }
        catch (Exception e)
        {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    private void attachJavaToXML()
    {
        try
        {
            objectDialog =new Dialog(this);
            objectDialog.setContentView(R.layout.please_wait_dialog);

            objectToolbar=findViewById(R.id.allNotifications_tollbar);
            setSupportActionBar(objectToolbar);
            objectRecyclerView=findViewById(R.id.allNotifications_RecyclerView);

            objectToolbar.inflateMenu(R.menu.all_notifications_menu);


            objectToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId())
                    {
                        case R.id.allNotifications_item_clear:
                            clearAllNotifications();
                            return true;
                        case R.id.allNotifications_item_goBack:
                            startActivity(new Intent(AllNotifications.this,MainContentPage.class));
                            return true;
                    }
                    return false;
                }
            });
        }
        catch (Exception e)
        {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
}

【问题讨论】:

    标签: java android xml android-studio android-toolbar


    【解决方案1】:

    尝试用以下代码替换工具栏 xml 代码:

    <LinearLayout 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_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">
    
    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/allNotifications_tollbar"
        android:layout_alignParentTop="true"
        android:background="#5A6E64"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    
        />
    
    
    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_margin="2dp"
        android:id="@+id/allNotifications_RecyclerView"/>
    
     </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      这里有两个选项。

      首先将RelativeLayout改为Linear布局或添加 android:layout_below ="@+id/allNotifications_tollbar" 到回收站视图。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多