与动作条自定义布局appcompat导致内容与操作栏重叠 [英] Custom layout with ActionBar in appcompat causes content to overlap with action bar

查看:149
本文介绍了与动作条自定义布局appcompat导致内容与操作栏重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了自定义的操作栏布局 AppCompat

I have implemented a custom action bar layout with 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 片段。这里的片段code:

My activity is just a dumb ActionBarActivity that loads a fragment in a FrameLayout. Here's the fragment code:

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

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

下面是结果的果冻豆与姜饼(Galaxy Nexus的为第一,模拟器后者):

Here's the result in Jelly Bean vs. Gingerbread (Galaxy Nexus for the first, emulator for the latter):

很抱歉的质量,动画PNG没有正常工作,所以我切换到GIF动画。

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

As you can see, the content layout is going over the action bar custom layout (notice the blue overflow in JB and scrollbar position).

使用非定制操作栏布局正常工作,在两个JB和GB。

Using a non-custom action bar layout works properly on both JB and GB.

推荐答案

叠加由于用于引用不同的Andr​​oid版本的内容针对不同的资源ID引起的。请参考Shellom的详细信息,的职位。同时,下面的代码片段应帮助您确定您的code中的相关部分。

The overlay is caused due the different resource id used to reference the content view in different versions of Android. Please refer to the post of Shellom for detailed information. Meanwhile, the following snippet should help you to identify the relevant portion of your code.

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


更新:开关不再需要。您可以更新的 appcompat-V7修订19.0.0。或更新,然后引用 android.R.id.content 在所有的Andr​​oid版本。


Update: The switch is no longer needed. You can update appcompat-v7 to revision 19.0.0. or newer and then reference android.R.id.content on all Android versions.

这篇关于与动作条自定义布局appcompat导致内容与操作栏重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆