安卓渐变色状态栏

节选自:Android实现状态栏(statusbar)渐变效果的示例 (有改动)


private View statusBarView;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
          initStatusBar();
        }
      });
  }
  private void initStatusBar() {
    if (statusBarView == null) {
      int identifier = getResources().getIdentifier("statusBarBackground", "id", "android");
      statusBarView = getWindow().findViewById(identifier);
    }
    if (statusBarView != null) {
        statusBarView.setBackgroundResource("你的渐变drawable资源id");
    }
  }

发表评论