【发布时间】:2017-11-16 12:14:13
【问题描述】:
活动有 2 个片段。当第一个 Fragment 可见时,我希望状态栏是半透明的(这里没有工具栏)。这按预期工作。
但是当我切换到第二个 Fragment 时,我希望状态栏是不透明的。工具栏应绘制在状态栏下方。我希望以编程方式完成这些更改,因为我无法在 Activity XML 中进行。
到目前为止,我已经尝试了以下代码。但这不起作用。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
Window window = getActivity().getWindow();
view.setFitsSystemWindows(true);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_LAYOUT_FLAGS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
window.addFlags(WindowManager.LayoutParams.TYPE_STATUS_BAR);
window.setStatusBarColor(ContextCompat.getColor(getContext(), R.color.colorPrimaryDark));
}
这是我得到的屏幕:
但我希望状态栏具有primaryDark 颜色并且工具栏显示在其下方。
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.blogspot.smarphy.pierecipe">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true">
<activity
android:name=".SplashActivity"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HomeyActivity"
android:label="@string/title_activity_homey"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
<activity
android:name=".RecipesCardsActivity"
android:label="@string/title_activity_recipes_cards"
android:parentActivityName=".HomeyActivity"
android:theme="@style/AppTheme.OpaqueStatus" />
<service android:name=".Services.HomeCardsNetworkService" />
<service android:name=".Services.SearchCardsNetworkService" />
</application>
</manifest>
【问题讨论】:
标签: android android-layout android-fragments android-xml