如何更改片段(Fragment)的状态栏颜色?

16
我有一个活动,在这个活动中,我使用五个片段来处理用户的触摸事件。我想为其中三个片段设置状态栏颜色,并为其它两个片段设置无状态栏。
如果我在一个片段的onCreateView方法中使用以下代码,则会更改活动的状态栏,并且对于所有片段,我都会得到白色的状态栏,因为我使用了getActivity()方法。因此,我正在寻找片段的解决方案。
getActivity().getWindow.setStatusBatColor(Color.White); 
我也尝试在style.xml中设置我的主题,并将其应用于XML中的片段根布局,但它不起作用。
<style name="StatusBarColor" parent="AppBaseTheme">
    <item name="android:statusBarColor">#F5F5F5
    </item>
</style>

fragment1.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:theme="@style/StatusBarColor"
android:background="@color/home_bg_white"
tools:context="mycontext">

非常感谢您提前提供的帮助。

4个回答

31
如果您想通过编程方式更改状态栏颜色(并且设备具有Android 5.0),则可以使用Window.setStatusBarColor()。不管活动是从Activity还是ActionBarActivity派生的,都不应该有任何影响。
只需尝试执行以下操作:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(Color.BLUE);
}

刚才在ActionBarActivity上测试过,一切正常。

注意:如果在你的values-v21样式文件中已经设置了FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS标志,则无需以编程方式进行设置:

<item name="android:windowDrawsSystemBarBackgrounds">true</item>

编辑2:

在您的活动中创建一个方法。

public void updateStatusBarColor(String color){// Color must be in hexadecimal fromat
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Color.parseColor(color));
    }
}

在您的Fragment中调用此方法。

((ActivityName)getActivity()).updateStatusBarColor("#000000")

我想改变片段状态栏,所以在片段类中不能添加getWindow方法,这就是问题所在。 - Aalap Patel
告诉我一件事,您是一次显示单个片段还是多个片段? - Himanshu Shekher Jha
不要单个片段,然后根据用户幻灯片屏幕跳转到不同的片段。 - Aalap Patel
让我们在聊天中继续这个讨论 - Himanshu Shekher Jha
声明一个Activity activity; activity = this; // 赋值 Window window = activity.getWindow(); @AalapPatel - Gayathri
显示剩余2条评论

9

下面是最简单的操作:

getActivity().getWindow().setStatusBarColor(getActivity().getColor(R.color.white));

该代码片段可以设置应用程序状态栏的颜色为白色。

3

KOTLIN解决方案:

当您创建一个活动和对应的片段(1个活动对多个片段的方案)时,首先在清单中设置一个主题(可以自动设置),该主题及其所有属性都将应用于所有活动的片段,除非您在所需片段中覆盖了特定的属性。在您的情况下,该属性是状态栏颜色。

要更改状态栏颜色,请在您的片段的onCreate方法中放置以下代码:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        requireActivity().window.statusBarColor = Color.GREEN
 
    }

但是如果您在您的主题样式中放置了以下内容:

<item name="android:windowTranslucentStatus">true</item>

你的主题xml:

<style name="Theme.MyTheme.NoActionBar" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/red_2091765</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    
    <item name="android:statusBarColor"tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <item name="android:windowTranslucentStatus">true</item> //HERE
    <item name="android:windowTranslucentNavigation">true</item>


</style>

如果你没有先清除标志,或者说你需要通过覆盖windowTranslucentStatus来实现,那么状态栏的颜色将不会被改变:

requireActivity().window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)

如果你已经定义了windowTranslucentStatus = true,那么最终的代码如下:

 override fun onCreate(savedInstanceState: Bundle?) {
                super.onCreate(savedInstanceState)
                 
                requireActivity().window.clearFlags
                   (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
                requireActivity().window.statusBarColor = Color.GREEN
         
            }

现在,如果您想对底部导航执行相同的操作,则模式相同,以此类推。

0

在你的片段 Java 类中只需编写这一行代码

((AppCompatActivity)requiereActivity()).getWindow().setStatusBarColor(R.color.white);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接