appcompat 中使用 ActionBar 的自定义布局会导致内容与操作栏重叠

2024-03-15

我已经实现了自定义操作栏布局AppCompat:

public class DoneBarActivity {
  public interface OnSaveActionListener {
      public void onSave();
  }

  public static void setupActionBar(final ActionBarActivity activity, 
                                    final OnSaveActionListener listener) {

      // Inflate a "Done/Cancel" custom action bar view.
      final LayoutInflater inflater = (LayoutInflater) activity
          .getSupportActionBar().getThemedContext()
          .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      final View customActionBarView = inflater.inflate(
          R.layout.actionbar_custom_view_done_cancel, null);
      customActionBarView.findViewById(R.id.actionbar_done)
          .setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              // "Done" (or "Save")
              if (listener != null) {
                  listener.onSave();
              }
              activity.finish();
          }
      });
      customActionBarView.findViewById(R.id.actionbar_cancel)
          .setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              // "Cancel"
              activity.finish();
          }
      });

      // Show the custom action bar view and 
      // hide the normal Home icon and title.
      final ActionBar actionBar = activity.getSupportActionBar();
      actionBar.setDisplayOptions(
          ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM | 
          ActionBar.DISPLAY_SHOW_HOME | 
          ActionBar.DISPLAY_SHOW_TITLE);
      actionBar.setCustomView(customActionBarView, 
                              new ActionBar.LayoutParams(
                                  ViewGroup.LayoutParams.MATCH_PARENT, 
                                  ViewGroup.LayoutParams.MATCH_PARENT
                              )
                             );
  }
}

我的活动只是一个愚蠢的活动ActionBarActivity加载一个片段FrameLayout。这是片段代码:

@Override
public View onCreateView(final LayoutInflater inflater, 
                         final ViewGroup container, 
                         final Bundle savedInstanceState) {
    // ...

    DoneBarActivity.setupActionBar((ActionBarActivity) getActivity(), 
                                   new DoneBarActivity.OnSaveActionListener() {
            @Override
            public void onSave() {
                    saveIssueChangesAndClose();
            }
    });
    return v;
}

这是操作栏布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="horizontal"
              android:divider="?attr/dividerVertical"
              android:showDividers="middle"
              android:dividerPadding="12dp">

    <include layout="@layout/include_cancel_button"/>
    <include layout="@layout/include_done_button"/>

</LinearLayout>

这是两个按钮:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             style="?actionButtonStyle"
             android:id="@+id/actionbar_cancel"
             android:layout_width="0dp"
             android:layout_height="match_parent"
             android:layout_weight="1">

    <TextView
            style="?actionBarTabTextStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingRight="20dp"
            android:drawableLeft="@drawable/ic_action_cancel"
            android:drawablePadding="8dp"
            android:gravity="center_vertical"
            android:text="@android:string/cancel"/>

</FrameLayout>

and

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             style="?actionButtonStyle"
             android:id="@+id/actionbar_done"
             android:layout_width="0dp"
             android:layout_height="match_parent"
             android:layout_weight="1">

    <TextView
            style="?actionBarTabTextStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingRight="20dp"
            android:drawableLeft="@drawable/ic_action_done"
            android:drawablePadding="8dp"
            android:gravity="center_vertical"
            android:text="@string/save"/>

</FrameLayout>

这是 Jelly Bean 与 Gingerbread 的结果(第一个是 Galaxy Nexus,后者是模拟器):

抱歉,质量问题,动画 PNG 无法正常工作,所以我改用动画 GIF。

正如您所看到的,内容布局正在超越操作栏自定义布局(请注意 JB 和滚动条位置中的蓝色溢出)。

使用非自定义操作栏布局在 JB 和 GB 上都可以正常工作。


覆盖是由于不同版本的 Android 中用于引用内容视图的资源 id 不同而导致的。请参阅Shellom 的帖子了解详细信息 https://stackoverflow.com/a/17998802/356895。同时,以下代码片段应该可以帮助您识别代码的相关部分。

// http://code.google.com/p/android/issues/detail?id=58108
private static int getContentViewCompat() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH ?
            android.R.id.content : R.id.action_bar_activity_content;
}

Update:不再需要该开关。您可以更新appcompat-v7 至修订版 19.0.0。或更新的然后参考android.R.id.content在所有 Android 版本上。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

appcompat 中使用 ActionBar 的自定义布局会导致内容与操作栏重叠 的相关文章

  • fresco 的 Proguard 错误

    我正在使用 ProGuard 当我在发布配置中运行项目时 出现以下错误 Warning com facebook imagepipeline bitmaps DalvikBitmapFactory can t find referenced
  • 如何在 StateListDrawable 中设置可绘制对象的 alpha 值?

    我想在按下时更改可绘制对象的 alpha 值 因此 我创建了两个可绘制对象并将它们放入 StateListDrawable 中 并设置按下状态的 alpha 值 但它就是行不通 StateListDrawable content new S
  • ActionBarCompat 支持库 android:selectableItemBackground 不起作用

    我正在使用新的 ActionBarCompat 支持库 操作栏中的操作按钮在按下时应更改其背景 它适用于 Android 4 3 但不适用于 Gingerbread 在姜饼中 如果我按下按钮 它不会改变背景 我什至改变了选择器 它再次适用于
  • 如何使用 (a)smack 在 Android 上保持 XMPP 连接稳定?

    我使用适用于 Android 的 asmack android 7 beem 库 我有一个后台服务正在运行 例如我的应用程序保持活动状态 但 XMPP 连接迟早会在没有任何通知的情况下消失 服务器表示客户端仍然在线 但没有发送或接收数据包
  • 在屏幕上随机生成一个圆圈并将其设为绿色或红色

    所以我一直在尝试制作一个游戏应用程序 它可以在 Android 屏幕上随机显示带有文本的红色按钮或带有文本的绿色按钮 如果有人可以帮助我 我将不胜感激 另外 如果有人知道如何做到这一点 我想慢慢地产生更快的酷优势 谢谢 SuppressLi
  • android - 过度绘制布局允许通过 LinearLayout 进行触摸

    在下面的 UI 中 我将下面的 drabable 覆盖了整个屏幕 LinearLayout 是透明的 并允许其下方的控件可单击或可触摸 基本上我可以滚动此 LinearLayout 下面的列表以及单击控件 我如何禁用它 See attach
  • 将搜索结果更新为 Android 中的 Lazy Adapter

    我有项目列表 想为其实现搜索功能 因此 我有一个带有 addTextChangedListener 的文本框 搜索结果运行良好 但当我尝试将结果设置为 ListView 时 新结果将附加到旧结果中 我正在使用惰性适配器 如何清除适配器中的旧
  • MAT(Eclipse 内存分析器)- 如何从内存转储中查看位图

    I m analyzing memory usage of my Android app with help of Eclipse Memory Analyzer http www eclipse org mat also known as
  • BluetoothLeScanner 服务内部问题

    Update从Android 10以上我认为你需要ACCESS BACKGROUND LOCATION权限 因此 如果此代码在最新的 Android 版本上不起作用 就是针对此问题的 ACCESS BACKGROUND LOCATION 受
  • 如何使用 onSearchRequested() 调用搜索对话框

    我正在尝试实现搜索对话框 但无法显示活动中的搜索 我在清单文件中定义了主要活动 此活动向用户显示了他们必须从中选择的选项列表 选项之一是 搜索 选项
  • 使用startActivityForResult,如何获取子活动中的requestCode?

    我有四项活动 即 A B C 和 D 我的情况是A将通过startActivityForResult启动活动B startActivityForResult new Intent this B class ONE 在另一种情况下 我将使用不
  • Android框架结构与MFC/Win32结构的比较?

    我为 Android 和 Windows 进行开发 使用 MFC 有时使用 win32 昨天我随意比较了这两个框架 它们显然非常非常不同 因此 Windows 开发与 Android 开发有很大不同 我想知道人们认为 Android 这样的
  • UnsupportedOperationException:特权进程中不允许使用 WebView

    我在用android sharedUserId android uid system 在我的清单中获得一些不可避免的权利 从 HDMI 输入读取安卓盒子 http eweat manufacturer globalsources com s
  • 在Android的activity中调用onResume

    在活动的过程中通过调用 this OnResume 强制 onResume 事件可以吗 或者我应该实现另一个由 OnResume 和第一个成员调用的过程 实现在您的重写中调用的另一个过程onResume 后者不打算由您调用 它是一种方便的方
  • 如果联系人与电话通讯录中的应用程序关联,则显示应用程序图标

    我正在尝试显示与该应用程序关联的电话号码的应用程序图标 我试着跟随this http www c99 org 2010 01 23 writing an android sync provider part 1 链接但是太难了 有没有任何库
  • Android 将菜单项在操作栏中向左对齐

    我的应用程序中有一个操作栏 它显示我定义的菜单项res menu activity main xml 我的菜单项在操作栏上向右对齐 我希望它们左对齐 我为此找到的唯一解决方案使用了自定义操作栏 如下所示 将菜单项放置在 Honeycomb
  • MPAndroidChart:组合图表

    我在用MPAndroidChart 库 https github com PhilJay MPAndroidChart 我想用CombinedChart创建这样的图表 那可能吗 我尝试了一下 但似乎不起作用 因为 这些条目没有按我的预期工作
  • 协程和 Firebase:如何实现类似 Javascript 的 Promise.all()

    在 Javascript 中 您可以同时启动两个 或更多 异步任务 等待它们完成 然后执行某些操作 继续 const firstReturn secondReturn await Promise all firstPromise secon
  • RecyclerView元素更新+异步网络调用

    我有一个按预期工作的回收视图 我的布局中有一个按钮可以填充列表 该按钮应该进行异步调用 根据结果 我更改按钮的外观 这一切都发生得很好 但是 当我单击按钮并快速向下滚动列表时 异步调用的结果会更新新视图的按钮 代替旧视图的视图 我该如何处理
  • Android:列“_id”不存在

    我收到这个错误 IllegalArgumentException 列 id 不存在 当使用SimpleCursorAdapter从我的数据库中检索 该表确实有这个 id柱子 注意到这是一个常见问题 我尝试根据网上的一些解决方案来解决它 但它

随机推荐