【问题标题】:How to change the background of the status bar to a gradient without affecting the navigation bar如何在不影响导航栏的情况下将状态栏背景改为渐变
【发布时间】:2018-01-29 11:11:04
【问题描述】:

我正试图让我的状态栏显示一个与它下面的背景完全一样的渐变,就像在这个问题中一样:How to apply gradient to status bar in android?

但是,该问题的答案导致整个布局延伸到导航栏,这是我想避免的。

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

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

【问题讨论】:

    标签: android android-layout android-windowmanager android-statusbar


    【解决方案1】:

    实现目标的一种方式。

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

    这里是示例代码。

    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);
    }
    

    【讨论】:

      【解决方案2】:

      对于这种类型的任务。您必须在您的活动或片段中设置第一个 FLAG_LAYOUT_NO_LIMITS 标志为,

      @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_cart);
              getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
          }
      

      现在,在您的 XML 中,只需在布局顶部设置一个 view,例如,

      <?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>
      

      因此,顶视图取代了布局中的状态栏,您可以根据需要设置视图背景。在上面view,我只是用了黑色,你也可以设置一个渐变作为背景。

      编辑

      但我们也必须动态设置view,因为每个设备上状态栏的高度根据屏幕大小不同。因此,您计算的状态栏高度为,

      public int getStatusBarHeight() {
              int result = 0;
              int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
              if (resourceId > 0) {
                  result = getResources().getDimensionPixelSize(resourceId);
              }
              return result;
          } 
      

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

      【讨论】:

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

      函数 setGradientStatusBar() {

          let gradientLayer = CAGradientLayer()
          gradientLayer.frame = CGRect(x:0, y:-20, width:375, height:64)
          let colorTop = UIColor(red: 85/255, green: 66/255, blue: 35/255, alpha: 1.0).cgColor
          let colorBottom = UIColor(red: 169/255, green: 132/255, blue: 69/255, alpha: 1.0).cgColor
          gradientLayer.colors = [ colorTop, colorBottom]
          gradientLayer.locations = [ 0.0, 1.0]
          gradientLayer.frame = CGRect(x:0, y:0, width:UIScreen.main.bounds.width, height:UIApplication.shared.statusBarFrame.height)
          let window: UIWindow? = UIApplication.shared.keyWindow
          let view = UIView()
          view.backgroundColor = UIColor.clear
          view.translatesAutoresizingMaskIntoConstraints = false
          view.frame = CGRect(x: 0, y: 0, width: (window?.frame.width)!, height: self.view.frame.height * 0.07)
          view.layer.addSublayer(gradientLayer)
          window?.addSubview(view)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-08-03
        • 2017-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多