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 
版权声明:本文为博主原创文章,转载请附上博文链接!
相关文章
|
2天前
|
JavaScript 前端开发 Java
FFmpeg开发笔记(四十七)寒冬下安卓程序员的几个技术转型发展方向
IT寒冬使APP开发门槛提升,安卓程序员需转型。选项包括:深化Android开发,跟进Google新技术如Kotlin、Jetpack、Flutter及Compose;研究Android底层框架,掌握AOSP;转型Java后端开发,学习Spring Boot等框架;拓展大前端技能,掌握JavaScript、Node.js、Vue.js及特定框架如微信小程序、HarmonyOS;或转向C/C++底层开发,通过音视频项目如FFmpeg积累经验。每条路径都有相应的书籍和技术栈推荐,助你顺利过渡。
12 3
FFmpeg开发笔记(四十七)寒冬下安卓程序员的几个技术转型发展方向
|
3天前
|
移动开发 开发工具 Android开发
探索安卓与iOS开发的差异:技术选择的影响
【8月更文挑战第17天】 在移动应用开发的广阔天地中,安卓和iOS两大平台各领风骚。本文通过比较这两个平台的编程语言、开发工具及市场策略,揭示了技术选择对开发者和产品成功的重要性。我们将从开发者的视角出发,深入探讨不同平台的技术特性及其对项目实施的具体影响,旨在为即将步入移动开发领域的新手提供一个清晰的指南,同时给予资深开发者新的思考角度。
|
4天前
|
vr&ar Android开发 iOS开发
探索安卓和iOS开发的未来趋势
在移动应用开发的广阔天地里,安卓和iOS两大平台如同双子星座般璀璨夺目。随着技术的不断进步,这两个平台的开发趋势也在悄然发生着变化。本文将带你一探究竟,看看未来安卓和iOS开发将会迎来哪些令人激动的新特性和挑战。让我们一起跟随技术的脚步,开启这场探索之旅吧!
|
4天前
|
Java Android开发 iOS开发
探索安卓与iOS开发的差异:从新手到专家的旅程
本文将带你走进移动应用开发的两大平台——安卓和iOS,揭示它们之间的主要差异。我们将从新手的视角出发,逐步深入到更复杂的技术层面,帮助你理解这两个平台的开发环境、编程语言和用户界面设计等方面的不同。无论你是刚入门的新手,还是有一定经验的开发者,这篇文章都将为你提供有价值的见解和建议。现在,让我们一起开始这段探索之旅吧!
|
4天前
|
搜索推荐 Android开发 iOS开发
探索安卓与iOS开发的差异之美
在数字时代的浪潮中,移动应用开发如同一场精心编排的交响乐,安卓和iOS这两大平台扮演着不同乐器的角色,各自以独特的方式奏响。本文将带领读者走进这场音乐盛宴,感受两大平台在开发过程中所展现的不同韵律,从设计理念到用户体验,从市场占有率到生态系统,我们将一探究竟,欣赏它们如何在竞争激烈的市场中和谐共存,共同推动技术的进步与创新。
12 0
|
8天前
|
编解码 Android开发 iOS开发
探索安卓与iOS开发的差异:平台选择对项目成功的影响
在移动应用开发的世界中,安卓和iOS是两大主导力量。本文深入探讨了这两个平台在开发过程中的主要差异,并分析了这些差异如何影响项目的成功。通过对比分析,我们旨在为开发者提供决策时的参考,帮助他们根据项目需求和目标用户群体做出最合适的平台选择。
|
6天前
|
Java Android开发 iOS开发
探索安卓与iOS开发的差异:平台选择对项目成功的影响
在移动应用开发的世界中,选择正确的平台是关键。本文通过比较安卓和iOS开发的核心差异,揭示平台选择如何影响应用的性能、用户体验和市场覆盖。我们将深入探讨各自的开发环境、编程语言、用户界面设计原则以及发布流程,以帮助开发者和企业做出明智的决策。
27 9
|
6天前
|
Java 开发工具 Android开发
探索安卓与iOS开发的差异:从新手到专家的旅程
在数字时代的浪潮中,移动应用开发成为了连接世界的桥梁。本文将带你走进安卓与iOS这两大移动操作系统的开发世界,通过比较它们的编程语言、开发工具和环境、用户界面设计以及市场分布等方面,揭示各自的独特之处。无论你是初涉编程的新手,还是寻求进阶的开发者,这篇文章都将为你提供宝贵的洞见,助你在移动应用开发的征途上一帆风顺。
20 5
|
5天前
|
移动开发 Java Android开发
安卓与iOS开发:异同探析
在移动应用开发的广阔天地中,安卓和iOS两大平台各自占据半壁江山。本文旨在深入探讨这两个平台在开发环境、编程语言、用户界面设计、性能优化及市场分布等方面的异同,为开发者提供实用的比较视角和决策参考。通过对比分析,我们不仅能更清晰地认识到各平台的特性,还能洞察未来移动开发的可能趋势。