Android开发学习之使用Toolbar实现不同的Fragment使用不同颜色的标题栏与状态栏

简介: Android开发学习之使用Toolbar实现不同的Fragment使用不同颜色的标题栏与状态栏                                            先看效果图。

Android开发学习之使用Toolbar实现不同的Fragment使用不同颜色的标题栏与状态栏

                                          

先看效果图。就像上面两幅图片一样,当我们点击下面的导航栏按钮时,APP会切换Fragment,且会更改标题栏和状态栏的颜色。这篇文章主要便是记录该效果的实现过程。

activity的布局如下

<?xml version="1.0" encoding="utf-8"?>
 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <FrameLayout
        android:id="@+id/frame"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
 
    </FrameLayout>
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000000"/>
 
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:background="#fff"
        android:layout_height="50dp">
 
        <LinearLayout
            android:id="@+id/one_lin"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:gravity="center"
            android:layout_height="match_parent">
            <ImageView
                android:id="@+id/one_img"
                android:layout_width="30dp"
                android:src="@drawable/fk"
                android:layout_height="30dp" />
        </LinearLayout>
        <LinearLayout
            android:id="@+id/two_lin"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:gravity="center"
            android:layout_height="match_parent">
            <ImageView
                android:id="@+id/two_img"
                android:layout_width="30dp"
                android:src="@drawable/fl"
                android:layout_height="30dp" />
        </LinearLayout>
        <!--<LinearLayout-->
            <!--android:id="@+id/three_lin"-->
            <!--android:layout_weight="1"-->
            <!--android:layout_width="match_parent"-->
            <!--android:gravity="center"-->
            <!--android:layout_height="match_parent">-->
            <!--<ImageView-->
                <!--android:id="@+id/three_img"-->
                <!--android:layout_width="30dp"-->
                <!--android:src="@drawable/bg_three"-->
                <!--android:layout_height="30dp" />-->
        <!--</LinearLayout>-->
 
    </LinearLayout>
 
</LinearLayout>
设置one_lin和two_lin的监听事件,当点击时显示对应的fragment,其中setWindowStatusBarColor为控制状态栏颜色的函数,ScanFragment和MyFragment两个类是我们继承Fragment实现的两个类,将在后面详细介绍。

public void onClick(View v) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        switch (v.getId()){
            case R.id.one_lin:
                setWindowStatusBarColor(0);
                if (myFragment != null)
                    transaction.hide(myFragment);
                if (scanFragment == null){
                    scanFragment = new ScanFragment();
                    transaction.add(R.id.frame, scanFragment);
                    transaction.show(scanFragment);
 
                }else{
                    transaction.show(scanFragment);
                }
                break;
            case R.id.two_lin:
                setWindowStatusBarColor(1);
                if (scanFragment != null)
                    transaction.hide(scanFragment);
                if (myFragment == null){
                    myFragment = new MyFragment();
                    transaction.add(R.id.frame, myFragment);
                    transaction.show(myFragment);
                }else{
                    transaction.show(myFragment);
                }
                break;
        }
        transaction.commit();
    }
状态栏颜色的修改在4.4和5.x环境下分别有不同的方式,低于4.4以下是不能修改的。而且对于不同定制系统也要做不同的适配,这里我暂时使用的是一个已经封装好的库(待测试适配性),只需在build.gradle文件的dependencies{}中添加

implementation 'com.githang:status-bar-compat:latest.integration'
setWindowStatusBarColor函数如下

 private void setWindowStatusBarColor(int i) {
        switch (i){
            case 0:
                StatusBarCompat.setStatusBarColor(this,  getResources().getColor(R.color.gray), true);
                break;
            case 1:
                StatusBarCompat.setStatusBarColor(this,  getResources().getColor(R.color.green), true);
                break;
        }
    }
接下来修改标题栏,默认的标题栏设置我们可以在res->values->styles.xml中看到,默认采用的是ActionBar。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
我们采用ToolBar代替ActionBar,首先修改parent属性为Theme.AppCompat.Light.NoActionBar,然后添加

<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
两个item,修改后代码如下

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
</style>
这里我们拿MyFragment作示例,MyFragment对应的布局文件如下

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar_me"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        app:titleTextColor="@android:color/black"
        android:background="@color/green"
        android:minHeight="?android:attr/actionBarSize">  <!-- 最小高度 -->
    </android.support.v7.widget.Toolbar>
</FrameLayout>
在xml文件中添加 android.support.v7.widget.Toolbar,这里需要注意的是我们需要使用android.support.v7.widget.Toolbar,否则过低版本则不可用。

MyFragment代码如下

public class MyFragment extends Fragment {
    @Nullable
    private AppCompatActivity mActivity;
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.me_layout, null);
        return view;
    }
 
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (AppCompatActivity) getActivity();
    }
 
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
 
    }
 
    @Override
    public void onResume() {
        super.onResume();
        Toolbar mToolbar = mActivity.findViewById(R.id.toolbar_me);
        mActivity.setSupportActionBar(mToolbar);
    }
}
需要在onCreateView返回布局的view;onAttach是fragment与activity绑定后调用的函数,之前是获取不到绑定的Activity的;onActivityCreated函数调用时,activity的onCreate函数已经执行完毕,此时我们可以进行ui操作;onResume函数执行时用户可以与fragment进行交互。因此我们在onResume函数里通过

Toolbar mToolbar = mActivity.findViewById(R.id.toolbar_me);
mActivity.setSupportActionBar(mToolbar);
获取我们在xml文件中所写的Toolbar,然后通过setSupportActionBar将Toolbar转为ActionBar。

这样,我们就实现了当点击底部导航栏时,变换状态栏和标题栏的颜色。此外,我们还可以在Toolbar布局下添加其他控件实现自定义标题栏。
--------------------- 
作者:Magic1an 
来源:CSDN 
原文:https://blog.csdn.net/Magic1an/article/details/87723359 
版权声明:本文为博主原创文章,转载请附上博文链接!
相关文章
|
3天前
|
Linux 编译器 Android开发
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
在Linux环境下,本文指导如何交叉编译x265的so库以适应Android。首先,需安装cmake和下载android-ndk-r21e。接着,下载x265源码,修改crosscompile.cmake的编译器设置。配置x265源码,使用指定的NDK路径,并在配置界面修改相关选项。随后,修改编译规则,编译并安装x265,调整pc描述文件并更新PKG_CONFIG_PATH。最后,修改FFmpeg配置脚本启用x265支持,编译安装FFmpeg,将生成的so文件导入Android工程,调整gradle配置以确保顺利运行。
24 1
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
|
26天前
|
Java Android开发
Android 开发获取通知栏权限时会出现两个应用图标
Android 开发获取通知栏权限时会出现两个应用图标
12 0
|
1月前
|
XML 缓存 Android开发
Android开发,使用kotlin学习多媒体功能(详细)
Android开发,使用kotlin学习多媒体功能(详细)
102 0
|
1月前
|
设计模式 人工智能 开发工具
安卓应用开发:构建未来移动体验
【2月更文挑战第17天】 随着智能手机的普及和移动互联网技术的不断进步,安卓应用开发已成为一个热门领域。本文将深入探讨安卓平台的应用开发流程、关键技术以及未来发展趋势。通过分析安卓系统的架构、开发工具和框架,本文旨在为开发者提供全面的技术指导,帮助他们构建高效、创新的移动应用,以满足不断变化的市场需求。
18 1
|
1天前
|
网络协议 Shell Android开发
Android 深入学习ADB调试原理(1)
Android 深入学习ADB调试原理(1)
9 1
|
17天前
|
XML 开发工具 Android开发
构建高效的安卓应用:使用Jetpack Compose优化UI开发
【4月更文挑战第7天】 随着Android开发不断进化,开发者面临着提高应用性能与简化UI构建流程的双重挑战。本文将探讨如何使用Jetpack Compose这一现代UI工具包来优化安卓应用的开发流程,并提升用户界面的流畅性与一致性。通过介绍Jetpack Compose的核心概念、与传统方法的区别以及实际集成步骤,我们旨在提供一种高效且可靠的解决方案,以帮助开发者构建响应迅速且用户体验优良的安卓应用。
|
19天前
|
监控 算法 Android开发
安卓应用开发:打造高效启动流程
【4月更文挑战第5天】 在移动应用的世界中,用户的第一印象至关重要。特别是对于安卓应用而言,启动时间是用户体验的关键指标之一。本文将深入探讨如何优化安卓应用的启动流程,从而减少启动时间,提升用户满意度。我们将从分析应用启动流程的各个阶段入手,提出一系列实用的技术策略,包括代码层面的优化、资源加载的管理以及异步初始化等,帮助开发者构建快速响应的安卓应用。
|
19天前
|
Java Android开发
Android开发之使用OpenGL实现翻书动画
本文讲述了如何使用OpenGL实现更平滑、逼真的电子书翻页动画,以解决传统贝塞尔曲线方法存在的卡顿和阴影问题。作者分享了一个改造后的外国代码示例,提供了从前往后和从后往前的翻页效果动图。文章附带了`GlTurnActivity`的Java代码片段,展示如何加载和显示书籍图片。完整工程代码可在作者的GitHub找到:https://github.com/aqi00/note/tree/master/ExmOpenGL。
21 1
Android开发之使用OpenGL实现翻书动画
|
19天前
|
Android开发 开发者
Android开发之OpenGL的画笔工具GL10
这篇文章简述了OpenGL通过GL10进行三维图形绘制,强调颜色取值范围为0.0到1.0,背景和画笔颜色设置方法;介绍了三维坐标系及与之相关的旋转、平移和缩放操作;最后探讨了坐标矩阵变换,包括设置绘图区域、调整镜头参数和改变观测方位。示例代码展示了如何使用这些方法创建简单的三维立方体。
15 1
Android开发之OpenGL的画笔工具GL10
|
26天前
|
Android开发
Android开发小技巧:怎样在 textview 前面加上一个小图标。
Android开发小技巧:怎样在 textview 前面加上一个小图标。
12 0