Android 如何向片段中的工具栏添加功能
我知道以前也有人问过类似的问题,但出于某种原因,尽管我尝试了多种解决方案,但我还是无法让它起作用 基本上,我已经在片段XML文件中创建了一个工具栏,并将其所有组件放入项目菜单文件(底部导航菜单)。但是,当我点击菜单中的项目时,它们没有得到响应。我尝试添加一个log.d,以查看是否调用了setnavigationonclick函数。我在下面附上了我的代码Android 如何向片段中的工具栏添加功能,android,android-fragments,android-toolbar,Android,Android Fragments,Android Toolbar,我知道以前也有人问过类似的问题,但出于某种原因,尽管我尝试了多种解决方案,但我还是无法让它起作用 基本上,我已经在片段XML文件中创建了一个工具栏,并将其所有组件放入项目菜单文件(底部导航菜单)。但是,当我点击菜单中的项目时,它们没有得到响应。我尝试添加一个log.d,以查看是否调用了setnavigationonclick函数。我在下面附上了我的代码 import android.os.Bundle; import android.view.LayoutInflater; import and
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>`
`
您必须使用来处理菜单项setNavigationOnClickListener
仅用于左侧的导航按钮(仅当您调用为其设置图标时才会显示)。您必须使用它来处理菜单项setNavigationOnClickListener
仅用于左侧的导航按钮(仅当您调用为其设置图标时才会显示)。用户IanHanniballake发布了正确答案,这是我的更新代码
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.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.forwardButton:
customView.swapColor();
Log.d(TAG,"this is working");
default:
break;
}
return false;
}
});
return view;
}
}
用户IanHanniballake发布了正确答案,这是我的更新代码
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.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.forwardButton:
customView.swapColor();
Log.d(TAG,"this is working");
default:
break;
}
return false;
}
});
return view;
}
}
谢谢,我想我只是有点大脑冻结,我也意识到我的调色方法不起作用,但至少你解决了我的问题,谢谢。我已经相应地修改了代码以演示解决方案谢谢,我想我只是有点头脑僵硬,我也意识到我的交换颜色方法不起作用,但至少你解决了我的问题谢谢。为了演示解决方案,我相应地修改了代码