qq_41377386 2019-07-30 16:45 采纳率: 0%
浏览 483

如何得到StatusBarBackground

查阅文档发现有两个id

android.R.id.statusBarBackground
android.R.id.navigationBarBackground

使用findViewById,获取导航栏的可以得到,但要获取statusBar的总是为null,不知道为什么

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-10 20:57
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    在Android中,R.id.statusBarBackground 是用于获取状态栏背景的颜色。然而,R.id.navigationBarBackground 用于获取导航栏(顶部区域)的背景颜色。

    为了获取状态栏背景,你需要通过以下方式:

    1. 在布局文件中定义一个自定义的控件,并将它设置为android:background="?android:id/statusBarBackground"
    2. 使用getResources().getDrawable(R.drawable.your_image)来加载状态栏背景图片。

    示例代码如下:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/your_image"/>
    
    </FrameLayout>
    

    然后,在你的Activity或Fragment中使用 imageView1.getBackground() 获取状态栏背景图片,或者直接调用 imageView1.getDrawable() 来获取状态栏背景颜色。

    评论

报告相同问题?