android - 如何向片段中的工具栏添加功能
问题描述
我知道以前有人问过类似的问题,但由于某种原因,尽管尝试了多种解决方案,但我无法让它发挥作用。
基本上,我在片段 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>`
解决方案
您必须使用setOnMenuItemClickListener
来处理菜单项。setNavigationOnClickListener
仅适用于左侧的导航按钮(仅在您调用setNavigationIcon()
为其设置图标时才会显示)。
推荐阅读
- angular - 如何在下拉选项加载完成后确保下拉模型加载而不破坏角度 9 中代码的异步性质
- spring - javax.naming.NameNotFoundException:名称 [jdbc/jndiDS] 未绑定在此上下文中。找不到 [jdbc]
- python - 将观察数添加到水平箱线图 seaborn
- algorithm - 如何按顺序遍历BTree而不以迭代方式递归?
- asp.net - 在 ASP.NET IIS 中运行的算法比在控制台中慢?
- amazon-web-services - “Access-Control-Allow-Origin”标头未在 *.s3.amazonaws.com 子域上发送
- amazon-web-services - AWS 安全组与网络 ACL
- google-cloud-platform - 我可以使用 googleapiclient python 客户端复制`create-with-container` gcloud sdk 命令吗
- python - 如何在带有熊猫的数据框中显示等于给定值的匹配元素?
- python - 代码的左侧如何为计数和 bin_edges 赋值?谁能简要解释一下这段代码