当前位置: 首页 > 知识库问答 >
问题:

Android:透明状态栏与动态动作栏颜色和抽屉布局

桑博远
2023-03-14

我有一个带有抽屉布局的活动。我们的客户要求我们根据从抽屉布局中选择的项目,动态更改此活动的操作栏颜色和相应的状态栏颜色。这很容易做到。然而,我的问题是,当我动态更改状态栏颜色时,我无法保持状态栏透明。当我打开抽屉时,彩色状态栏会覆盖抽屉布局的顶部,如下所示:

36066840da.png" width="100%" height="100%" />

但是,我希望我的DrawerLayout看起来像这样:

这我可以用下面的行做:

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

然而,我的问题不是我不能设置状态栏的透明度。我的问题是,状态栏和操作栏颜色的动态更改不适用于windowTranslucentStatus。我的状态栏颜色仍然是colorPrimaryDark(上图中状态栏上可见的深黄色),即使在我调用getWindow()之后也是如此。setStatusBarColor()

现在,我遵循了这个教程,以及这个和这个stackoverflow问题以及其他许多问题,但无法解决这个问题。所有这些文章都说,一旦我将窗口半透明状态设置为,则操作栏将移动到状态栏下方的顶部(以便状态栏与操作栏重叠)。之后,我应该能够为动作栏添加一些填充,简单地更改动作栏的颜色也会导致相同颜色的状态栏变暗,因为状态栏实际上是半透明的,并且与我的动作栏重叠。然而,出于某种原因,我的情况并非如此。无论我是将fitsystemwindows设置为true还是false或完全删除该属性,操作栏都会保持不变。动作栏总是在状态栏的下方,当然,如果我设置透明度,状态栏总是黄色的。

我还尝试在以编程方式更改状态栏颜色时,将其设置为alpha。这确实让状态栏有点透明,但它看起来很奇怪,因为它不再是真正的黑暗。删除协调器布局也无济于事。我花了几个小时试图解决这个问题,现在非常沮丧。非常感谢您的帮助。

我的活动\u main

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true">

    <include layout="@layout/nav_header_main" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/nav_menu_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/nav_header_height"
        android:clipToPadding="false"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/size_14dp"
        app:layoutManager="LinearLayoutManager" />
</android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

下面是我的app_bar_main的XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    app:elevation="0dp">


    <FrameLayout
        android:id="@+id/toolbar_container"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary" />

        <com.quintype.sakshipost.Widgets.CustomMaterialSearchView
            android:id="@+id/search_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </FrameLayout>

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

共有3个答案

喻昀
2023-03-14

我在我的应用程序中这样做。

<style name="AppTheme" parent="Theme.AppCompat.Dark">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>

在重新启动活动的情况下,颜色主要将设置状态栏的颜色。

因此,将其设置为透明的黑色,然后在代码中重新启动活动。您可以在样式中使用不同的颜色主值创建多个样式,然后在活动中这样做。

//onClick or condition statement here
setTheme(R.style.AppThemeLight);
setContentView(R.layout.activity_link_manager_light);
restart();


public void restart(){
Intent i = getBaseContext().getPackageManager()
            .getLaunchIntentForPackage( getBaseContext().getPackageName() );
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(i);
}

我不认为你需要重置内容视图,除非你也在切换布局,不确定你是否可以尝试重置它到相同的布局,如果没有它就不能工作。

这对我来说绝对有效,但我从未尝试使用透明值。

你可以使用这种方法重置整个应用程序主题,并切换到一个新的布局,其中有不同颜色的硬编码,只需在布局上保持所有id的相同,在代码中,只要你在将ContentView设置为正确的布局后引用findViewByID,就可以获得正确的id。每个版面的id都不同,但在调用FindViewByd时,您将始终获得当前使用setContentView设置的版面id。

您还可以将当前主题保存到sharedpref文件中,并在调用setContentView之前使用开关在applaunch时设置主题(否则状态栏颜色不会改变)

考虑这个代码。顺便说一下,这已经接近onCreate的开始了

String Theme;
    SaveData = getSharedPreferences(SaveFileName,0);
    Theme = SaveData.getString("Theme","Default Theme");
switch(Theme){
        case "Light Theme":
            setTheme(R.style.AppThemeLight);
            setContentView(R.layout.activity_link_manager_light);
            ListTextViewID = R.id.AppListTextViewLight;
            ListRowID = R.layout.app_list_item_light;
            break;

我在设置contentview之前设置apptheme的方式就是为什么状态栏的颜色会改变。

邹曦之
2023-03-14

正如doc在这里所说,不能设置FLAG_Transparent_状态。

要使其生效,窗口必须使用FLAG_DRAWS_system_bar_backgrounds绘制系统栏背景,并且不能设置FLAG_Transparent_状态。

江阳夏
2023-03-14

您的问题是因为getWindow()。setStatusBarColor()与抽屉布局不兼容。为了使状态栏保持半透明并能够更改其颜色,您必须遵循以下过程:

v21/样式中添加/修改以下主题。xml文件如下:

<style name="AppTheme.NoActionBar" parent="AppTheme">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>

这是标准DrawerLayout使用的主题(当然,假设您在具有DrawerLayout的活动中使用此主题)。这将使状态栏半透明,正如您已经知道的。

接下来,从你的app_bar_main中的Coordinator布局中删除android:fitsystemwindows=“true”

接下来,无论在哪里更改工具栏/状态栏的颜色,请使用drawerLayout。setStatusBarBackground(colorDrawable)使用与工具栏相同的颜色(当然,假设对抽屉布局的引用被称为抽屉布局)。请注意,抽屉布局。setStatusBarBackground()方法采用可绘制的对象,与窗口不同。setStatusBarColor()方法,该方法采用int颜色,因此您可能需要使用以下方法将颜色转换为可绘制颜色:

new ColorDrawable(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))

这将确保你的状态栏是半透明的,并赋予它改变颜色的能力。希望你能做到这一点。

 类似资料:
  • 我在这个问题上做了一些研究,但我找不到一个完整的解决方案,所以,一步一步,通过一些尝试和错误,我终于找到了我们如何实现这些结果:拥有一个透明或彩色的和。看我下面的回答。

  • 因此,我在这里读了几篇关于更改状态栏文本颜色的帖子,但都没用。所以我最初的问题是,状态栏现在在iOS7中是透明的,我希望它是带黑色文本的白色。所以我把它添加到我的viewcontroller中。 好的,现在状态是黑对黑,不能阅读文本,但至少它不是透明的。所以现在我做了下面的事情。 进入plist并添加了这个 然后我将此添加到我的应用程序委托 现在我有一个状态栏是白色的,但是文本是白色的,所以你看不

  • 我想在android工作室的应用程序中创建这个操作栏和状态栏的界面

  • 只有在第一次打开应用程序时,半透明的状态栏才会出现问题。请看屏幕截图: http://i1335.photobucket.com/albums/w673/ductruongcntt/Screenshot_2014-06-26-14-17-26_zps1e9a56f4.png 以下是我使用的样式的XML,其中包括半透明状态栏: 我的主题是:

  • 在“新”Android4.3版中有一个新功能。屏幕顶部的状态栏在launcher中是透明的(我在Galaxy S3上使用三星TouchWiz),在其他一些应用中也是透明的(看起来有点像iOS 7风格)。你能告诉我如何在我自己的应用程序(Eclipse、Java)中使状态栏透明(或彩色)吗?

  • 我知道有成千上万个这样的问题,但是我尝试了所有的方法,但是我不能想出一个解决方案。基本上,我使用的是NoActionBar风格的活动。 风格。xml: v21/风格。xml: 家庭活动正常,抽屉将在透明状态栏下正常打开: 问题是使用以下布局的另一个活动: 基本上,状态栏是透明的,所以我看不到它,因为我的colorPrimary是白色的。奇怪的是,如果我折叠工具栏(因此只有选项卡可见),状态栏将正确