Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.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 Windowmanager_Android Statusbar - Fatal编程技术网

Android 如何将状态栏的背景更改为渐变而不影响导航栏

Android 如何将状态栏的背景更改为渐变而不影响导航栏,android,android-layout,android-windowmanager,android-statusbar,Android,Android Layout,Android Windowmanager,Android Statusbar,我试图让我的状态栏显示一个渐变,就像下面的背景一样,就像这个问题: 然而,这个问题的答案会导致整个布局延伸到导航栏中,这是我想要避免的 我找不到一种方法不使用WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS并使状态栏完全透明或显示渐变(与其下方的渐变相同) 有没有办法让状态栏显示渐变,而不影响导航栏?理想情况下,状态栏是“不可见”的,电源/wifi/etc图标仍然可见,但显示的颜色是当前活动布局的渐变。实现目标的一种方法 将窗口背景设置为渐变,状态

我试图让我的状态栏显示一个渐变,就像下面的背景一样,就像这个问题:

然而,这个问题的答案会导致整个布局延伸到导航栏中,这是我想要避免的

我找不到一种方法不使用WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS并使状态栏完全透明或显示渐变(与其下方的渐变相同)


有没有办法让状态栏显示渐变,而不影响导航栏?理想情况下,状态栏是“不可见”的,电源/wifi/etc图标仍然可见,但显示的颜色是当前活动布局的渐变。

实现目标的一种方法

将窗口背景设置为渐变,状态栏颜色为透明

下面是示例代码

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
     Window window = getWindow();
     Drawable background = getResources().getDrawable(R.drawable.bg_gradient); //bg_gradient is your gradient.
     window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
     window.setStatusBarColor(getResources().getColor(android.R.color.transparent));
     window.setBackgroundDrawable(background);
}

实现目标的一种方法

将窗口背景设置为渐变,状态栏颜色为透明

下面是示例代码

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
     Window window = getWindow();
     Drawable background = getResources().getDrawable(R.drawable.bg_gradient); //bg_gradient is your gradient.
     window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
     window.setStatusBarColor(getResources().getColor(android.R.color.transparent));
     window.setBackgroundDrawable(background);
}

对于这种类型的任务,您必须首先在活动或片段中设置
标志\u布局\u无限制
标志

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }
现在,在XML中,只需在布局顶部设置一个
视图

<?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:layout_height="match_parent"
    android:orientation="vertical">
    <View
        android:id="@+id/status_bar_view"
        android:background="#000"       
        android:layout_width="match_parent"
        android:layout_height="24dp"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="22dp">
                 //main layout here.
    </RelativeLayout>        
</LinearLayout>

上面的方法返回每个设备的状态栏高度。您可以将其设置为
视图的高度。因此,您的视图在大小上与状态栏完全相同。

对于此类任务。您必须首先在活动或片段中设置
标志布局限制
标志

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }
现在,在XML中,只需在布局顶部设置一个
视图

<?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:layout_height="match_parent"
    android:orientation="vertical">
    <View
        android:id="@+id/status_bar_view"
        android:background="#000"       
        android:layout_width="match_parent"
        android:layout_height="24dp"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="22dp">
                 //main layout here.
    </RelativeLayout>        
</LinearLayout>
上述方法返回每个设备的状态栏高度。您可以将其设置为
视图的高度。因此,您的视图在大小上与状态栏完全相同。

func setGradientStatusBar(){

func setGradientStatusBar(){


它可以工作,但如果我不删除导航栏怎么办?因为使用此代码,活动内容位于导航栏下方,但如果我不删除导航栏怎么办?因为使用此代码,活动内容位于导航栏下方