果冻豆android.R.id.content改变了吗?

时间:2012-07-26 08:51:51

标签: android android-layout android-actionbar android-4.2-jelly-bean

我最近在Jelly Bean设备上测试了我的App,发现我的Actionbar Dodging Code不再有用了。

我有一个透明的Actionbar,OverlayMode为true,但是想要在我的某些屏幕中像操作栏一样操作Actionbar。

为了使这项工作,我从Honeycomb Gallery Code

借用了一些代码

基本上我检查Acionbar高度并将android.R.id.content存储桶的topMargin设置为此值。

  public void setupActionbar() {
    final int sdkVersion = Build.VERSION.SDK_INT;
    int barHeight = getSupportActionBar().getHeight();
      if (sdkVersion < Build.VERSION_CODES.HONEYCOMB) {
        FrameLayout content = (FrameLayout) findViewById(android.R.id.content);
        RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) content.getLayoutParams();

        if (params.topMargin != barHeight) {
          params.topMargin = barHeight;
          content.setLayoutParams(params);
        }

        if (!getSupportActionBar().isShowing()) {
          params.topMargin = 0;
          content.setLayoutParams(params);
        }
      } else {
        FrameLayout content = (FrameLayout) findViewById(android.R.id.content);
        LayoutParams params = content.getLayoutParams();
        if (params instanceof RelativeLayout.LayoutParams) {
          android.widget.RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) params;
          if (lp.topMargin != barHeight) {
            lp.topMargin = barHeight;
            content.setLayoutParams(lp);
          }

          if (!getActionBar().isShowing()) {
            lp.topMargin = 0;
            content.setLayoutParams(lp);
          }
        } else if (params instanceof FrameLayout.LayoutParams) {
          FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) params;
          if (lp.topMargin != barHeight) {
            lp.topMargin = barHeight;
            content.setLayoutParams(params);
          }

          if (!getActionBar().isShowing()) {
            lp.topMargin = 0;
            content.setLayoutParams(params);
          }
        }

      }

在Jelly Beans中,由于某些原因,这种策略不再起作用了。 Jelly Bean是否更改了id.content Container perhabs?

1 个答案:

答案 0 :(得分:4)

好的回答我自己的问题。 首先,我的代码中有一个拼写错误:

content.setLayoutParams(params); should read
content.setLayoutParams(lp);

但真正的问题在于

FrameLayout content = (FrameLayout) findViewById(android.R.id.content);

提供整个屏幕的视图,包括状态栏仅适用于Android 4.1 Jelly Bean

View content = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);

给出需要在透明操作栏下推送的RootContentView。