gpt4 book ai didi

java - 我可以为每个 fragment 单独设置工具栏吗?如何处理抽屉导航

转载 作者:行者123 更新时间:2023-11-29 04:23:36 25 4
gpt4 key购买 nike

在我的应用程序中,某些页面在工具栏中具有自定义 View 。有的 fragment 有透明工具栏,有的 fragment 有坐标布局滚动。

所以我决定为我想知道的每个 fragment 单独设置工具栏,这是否是一个好习惯。

如果有人已经这样做了,请分享代码或示例。

最佳答案

您可以在 fragment 中使用自定义工具栏。并且您必须为每个 fragment 单独实现它们。首先在 fragment 布局中声明工具栏:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:gravity="start"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
>

<RelativeLayout
// your custom toolbar layout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>


</android.support.v7.widget.Toolbar>

然后在你的 fragment 中实现它:

在 fragment 中找到它:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle
savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);

//set toolbar appearance
toolbar.setBackground(your background);

//for create home button
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setSupportActionBar(toolbar);
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);

return view;
}

是的,您可以实现点击监听器以及您想要对工具栏执行的任何操作。有关更多信息,请查看第二个答案: How to get custom toolbar

关于java - 我可以为每个 fragment 单独设置工具栏吗?如何处理抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47628279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com