android - Jelly Bean android.R.id.content 改变了吗?

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

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

我有一个 OverlayMode 为 true 的透明 Actionbar,但我想在我的某些屏幕中将 Actionbar 表现得像实心 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 容器?

最佳答案

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

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

但真正的问题是

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

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

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

给出需要在Transparent Actionbar下推送的RootContentView。

关于android - Jelly Bean android.R.id.content 改变了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11665677/

相关文章:

android - 创建或解析Java和Kotlin分类成Gradle任务

java - 如何在 java android 中将 ellipsize 设置为两行或多行 textview

android - AVD Pixel_4_XL_API_30 的模拟器进程被终止

适配器中 ListView 的 Android 错误 : java. lang.ClassCastException

android - 如何阻止 RecyclerView 在主行下方显示空行?

java - ConstraintView 干扰显示键盘

java - Android - 在所有 Activity 中设置 ActionBar 颜色?

Android:ActionBar 自定义问题

java - Android操作栏,更改颜色并添加搜索功能

android - Phonegap 的 FileTransfer.upload() 在 Android 上抛出错误代码 3