如何向片段中的工具栏添加功能
How can you add functionality to toolbars within fragments
我知道以前有人问过类似的问题,但出于某种原因,尽管尝试了多种解决方案,但我还是无法解决这个问题。
基本上,我在片段 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
to handle menu items. setNavigationOnClickListener
is only for the navigation button on the left hand side (which only displays if you call setNavigationIcon()
为其设置图标。
用户 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;
}
}
我知道以前有人问过类似的问题,但出于某种原因,尽管尝试了多种解决方案,但我还是无法解决这个问题。
基本上,我在片段 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
to handle menu items. setNavigationOnClickListener
is only for the navigation button on the left hand side (which only displays if you call setNavigationIcon()
为其设置图标。
用户 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;
}
}