首页 > 解决方案 > 如何向片段中的工具栏添加功能

问题描述

我知道以前有人问过类似的问题,但由于某种原因,尽管尝试了多种解决方案,但我无法让它发挥作用。

基本上,我在片段 XML 文件中创建了一个工具栏,并将其所有组件放入项目菜单文件 (bottom_navigation_menu)。但是,当我单击菜单中的项目时,它们没有得到任何响应。我尝试添加一个 log.d,以查看是否正在调用 setnavigationonclick 函数,但不是。我在下面附上了我的代码。

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;


public class pneumothorax_fr_flashcard_view extends Fragment {
    private static final String TAG = "flashcard view";
    private pneumothorax_layout_flashcard_view customView;



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.pneumothorax_flashcard_view,container,false);
        customView = (pneumothorax_layout_flashcard_view) view.findViewById(R.id.flashcardView);
        Toolbar toolbar = (Toolbar)view.findViewById(R.id.toolbarBot);
        ((AppCompatActivity) getActivity()).getSupportActionBar();


        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customView.swapColor();
            }
        });


        return view;
    }



}

这是 XML,在这个文件中,我有一个工具栏和一个自定义视图,其中有一个绿色框,该框的颜色是为了在执行 onclick 时更改。

<?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"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">
            <androidx.appcompat.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/toolbarBot"
                app:menu="@menu/bottom_navigation_menu">
            </androidx.appcompat.widget.Toolbar>
        </com.google.android.material.appbar.AppBarLayout>

    </RelativeLayout>

   <RelativeLayout
       android:layout_height="match_parent"
       android:layout_width="wrap_content">
       <com.example.spidermed.pneumothorax_layout_flashcard_view
           android:id="@+id/flashcardView"
           android:layout_width="match_parent"
           android:layout_height="match_parent"/>
   </RelativeLayout>




</RelativeLayout>`

标签: androidandroid-fragmentsandroid-toolbar

解决方案


您必须使用setOnMenuItemClickListener来处理菜单项。setNavigationOnClickListener仅适用于左侧的导航按钮(仅在您调用setNavigationIcon()为其设置图标时才会显示)。


推荐阅读