Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 以编程方式使状态栏不透明_Android_Android Layout_Android Fragments_Android Xml - Fatal编程技术网

Android 以编程方式使状态栏不透明

Android 以编程方式使状态栏不透明,android,android-layout,android-fragments,android-xml,Android,Android Layout,Android Fragments,Android Xml,该活动有两个片段。当第一个片段可见时,我希望状态栏是半透明的(这里没有工具栏)。这是意料之中的事 但是当我切换到第二个片段时,我希望状态栏是不透明的。工具栏应绘制在状态栏下方。我希望这些更改以编程方式完成,因为我无法在活动XML中完成 到目前为止,我已经尝试了以下代码。但这不起作用 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Window window = getActivity().ge

该活动有两个片段。当第一个片段可见时,我希望状态栏是半透明的(这里没有工具栏)。这是意料之中的事

但是当我切换到第二个片段时,我希望状态栏是不透明的。工具栏应绘制在状态栏下方。我希望这些更改以编程方式完成,因为我无法在活动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>

我对您的理解是正确的

从片段1切换到片段2时,您希望从全屏切换到正常

如果不使用此选项,您的活动中可能会有此选项

android:theme="@style/FullscreenTheme"
在片段2中,您可以使用它来设置非全屏模式。 @可空

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
        getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        ....
    }
    @Override
    public void onDestroyView() {
        getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        ....
    }
添加非全屏时,应清除全屏标志

信息:
半透明状态栏是在API级别19中引入的

@Anand你能用清单中关于你活动的代码更新你的问题吗?请编辑check@Anand你是如何在片段一上设置全屏的?因为所有的活动都没有全屏主题,实际上不是全屏主题。只是做状态栏translucent@Anand检查我的最新答案。您需要清除半透明状态栏的标志。