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 
版权声明:本文为博主原创文章,转载请附上博文链接!
相关文章
|
8天前
|
IDE Android开发 iOS开发
探索Android与iOS开发的差异:平台选择对项目成功的影响
【9月更文挑战第27天】在移动应用开发的世界中,Android和iOS是两个主要的操作系统平台。每个系统都有其独特的开发环境、工具和用户群体。本文将深入探讨这两个平台的关键差异点,并分析这些差异如何影响应用的性能、用户体验和最终的市场表现。通过对比分析,我们将揭示选择正确的开发平台对于确保项目成功的重要作用。
|
7天前
|
Java Maven 开发工具
第一个安卓项目 | 中国象棋demo学习
本文是作者关于其第一个安卓项目——中国象棋demo的学习记录,展示了demo的运行结果、爬坑记录以及参考资料,包括解决Android Studio和maven相关问题的方法。
第一个安卓项目 | 中国象棋demo学习
|
5天前
|
开发框架 移动开发 Android开发
安卓与iOS开发中的跨平台解决方案:Flutter入门
【9月更文挑战第30天】在移动应用开发的广阔舞台上,安卓和iOS两大操作系统各自占据半壁江山。开发者们常常面临着选择:是专注于单一平台深耕细作,还是寻找一种能够横跨两大系统的开发方案?Flutter,作为一种新兴的跨平台UI工具包,正以其现代、响应式的特点赢得开发者的青睐。本文将带你一探究竟,从Flutter的基础概念到实战应用,深入浅出地介绍这一技术的魅力所在。
22 7
|
8天前
|
开发框架 前端开发 Android开发
安卓与iOS开发中的跨平台解决方案
【9月更文挑战第27天】在移动应用开发的广阔天地中,安卓和iOS两大操作系统如同双子星座般耀眼。开发者们在这两大平台上追逐着创新的梦想,却也面临着选择的难题。如何在保持高效的同时,实现跨平台的开发?本文将带你探索跨平台开发的魅力所在,揭示其背后的技术原理,并通过实际案例展示其应用场景。无论你是安卓的忠实拥趸,还是iOS的狂热粉丝,这篇文章都将为你打开一扇通往跨平台开发新世界的大门。
|
5天前
|
缓存 Java Linux
探索安卓开发:从新手到专家的旅程
【9月更文挑战第30天】在这篇文章中,我们将一起踏上一段激动人心的旅程,探索安卓开发的广阔世界。无论你是刚入门的新手,还是希望提升技能的开发者,本文都将为你提供宝贵的知识和指导。我们将深入探讨安卓开发的基础知识、关键概念、实用工具和最佳实践,帮助你在安卓开发领域取得更大的成功。让我们一起开启这段精彩的旅程吧!
|
6天前
|
监控 安全 Java
Kotlin 在公司上网监控中的安卓开发应用
在数字化办公环境中,公司对员工上网行为的监控日益重要。Kotlin 作为一种基于 JVM 的编程语言,具备简洁、安全、高效的特性,已成为安卓开发的首选语言之一。通过网络请求拦截,Kotlin 可实现网址监控、访问时间记录等功能,满足公司上网监控需求。其简洁性有助于快速构建强大的监控应用,并便于后续维护与扩展。因此,Kotlin 在安卓上网监控应用开发中展现出广阔前景。
9 1
|
9天前
|
搜索推荐 前端开发 Android开发
安卓开发中的自定义视图:打造个性化用户界面
【9月更文挑战第26天】在移动应用开发的广阔天地中,定制性是提升用户体验的不二法宝。本文将带你深入了解安卓开发中自定义视图的魅力所在,通过简洁明了的语言和直观的代码示例,展示如何从零开始创建属于自己的控件,让你的应用界面与众不同。
|
Android开发
Android实现沉浸式通知栏,通知栏可以根据app的颜色可改变啦
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/lyhhj/article/details/46547273 最近好多app都已经满足了沉浸式通知栏,所谓沉浸式通知栏:就是把用来导航的各种界面操作空间隐藏在以程序内容为主的情景中,通过相对“隐形”的界面来达到把用户可视范围最大化地用到内容本身上。
902 0
|
21天前
|
Android开发 开发者 Kotlin
探索安卓开发中的新特性
【9月更文挑战第14天】本文将引导你深入理解安卓开发领域的一些最新特性,并为你提供实用的代码示例。无论你是初学者还是经验丰富的开发者,这篇文章都会给你带来新的启示和灵感。让我们一起探索吧!
|
16天前
|
Android开发 开发者
安卓开发中的自定义视图:从入门到精通
【9月更文挑战第19天】在安卓开发的广阔天地中,自定义视图是一块充满魔力的土地。它不仅仅是代码的堆砌,更是艺术与科技的完美结合。通过掌握自定义视图,开发者能够打破常规,创造出独一无二的用户界面。本文将带你走进自定义视图的世界,从基础概念到实战应用,一步步展示如何用代码绘出心中的蓝图。无论你是初学者还是有经验的开发者,这篇文章都将为你打开一扇通往创意和效率的大门。让我们一起探索自定义视图的秘密,将你的应用打造成一件艺术品吧!
41 10
下一篇
无影云桌面