Android L下使用Feinstein SldingMenu和AppCompat v21的ActionBarActivity在屏幕底部被裁剪了。

4

我正在使用带有样式“ NoActionBar”的 AppCompat v21,并在 onCreate 中添加了一个操作/工具栏。

此外,还添加了Feinstein的SlidingMenu,这导致Activity(因此内部Fragment)与Android的导航按钮重叠(未完全显示,底部被切断)

如果我添加:

android:layout_marginBottom="48dp"

在布局中,一切都是可见的(当然)。

在Android 4.4上,一切都正常显示。

在使用支持库的Android L上,我错过了什么?

在onCreate中添加SlidingMenu:

super.onCreate(..)
setContentView(..)

menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.LEFT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
menu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
menu.setMenu(R.layout.menu);
menu.setBehindWidthRes(200dp);
解决方案: 问题在此处说明https://github.com/jfeinstein10/SlidingMenu/issues/680(包括解决方案)。
Slding Menu to SLIDING_CONTENT
OR: update the SlidingMenu source like mentioned in the link aboce

更好的解决方案: (对于5.0上的三星设备也适用)- 由withaay提供

Adding the following lines to the SlidingMenu constructors has worked for me. I didn't have to make any other code changes.

if(Build.VERSION.SDK_INT >= 21)
    setSystemUiVisibility(SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION)

请发布您的完整活动XML布局。 - Blacklight
@Blacklight 更新了问题(该片段已加载到容器中^^) - ddd
6个回答

8
将以下代码添加到SlidingMenu构造函数中对我有效。我不需要做任何其他更改。
if(Build.VERSION.SDK_INT >= 21)
    setSystemUiVisibility(SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);

一个更好的解决方案(也适用于三星设备上的Android 5.0 :)) - ddd
我使用了滑动菜单库,但无法编辑其构造函数,应该在哪里添加这行代码? - GOLDEE
@GOLDEE,它在SlidingMenu.java文件中。 - whitaay

2

0
尝试在您的工具栏中添加android:minHeight="?attr/actionBarSize"
 <android.support.v7.widget.Toolbar
      android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

0
你已经在主LinearLayout中添加了工具栏,但是你仍然在使用match_parent来设置FrameLayout容器的高度。尝试填充剩余的LinearLayout空间:
<FrameLayout
      android:id="@+id/container"
      android:layout_weight="1"
      android:layout_width="match_parent"
      android:layout_height="0dp" />

谢谢,但这对布局也没有影响。我必须提到,即使没有工具栏,它也太大了,超出了屏幕的范围。所以我认为这是一个普遍性的问题。 - ddd
我犯了一个错误。这是由于Feinstein的滑动菜单引起的。已更新问题。有什么想法吗? - ddd
你的问题中我没有看到任何关于那个方面的更新?对于滑动菜单,只需使用文档中提供的示例布局即可。我按照示例进行了操作,在安卓4.4和Lollipop上都运行良好,没有任何被截断的情况。 - Blacklight
抱歉,我对那个库完全不熟悉,但它可能与此有关。我只使用了标准的导航抽屉:http://developer.android.com/training/implementing-navigation/nav-drawer.html - Blacklight
谢谢你的帮助 :) 我一直在关注AppCompat和Activity,因为菜单没有显示任何错误...但是我发现了这个问题链接,可以像那里提到的那样解决。 - ddd

0
如果您正在尝试使用Fenstein菜单与App Compat,可能会遇到滑动菜单的问题。您的滑动菜单将从手机底部的后面滑出。
要解决此问题,请定位
YOUR-PROJECT/sliding-menu/src/com/jeremyfeinstein/slidingmenu/lib/SlidingMenu.java

查找

int bottomPadding = insets.bottom;

并将其替换

int bottomPadding = insets.bottom;
        if (Build.VERSION.SDK_INT >= 21) {
            Resources resources = getContent().getResources();
            int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
            if (resourceId > 0) {
                bottomPadding += resources.getDimensionPixelSize(resourceId);
            }
        }

我在努力搜索这个问题的解决方案,上面的方法对我有效。


是的,你说得对,谢谢。 这就是我之前发布的解决方案。 - ddd

0
以下代码对我有效:基本上,您始终调用setSlidingActionBarEnabled(false),然后根据下面的公式计算并设置根视图的正确高度,具体取决于您是否有导航栏。
public class MyActivity extends SlidingActivity {
...
    @Override
    public void onCreate(Bundle savedInstanceState) {    
...
        setSlidingActionBarEnabled(false);
        mBtmNavBarSize = getNavigationBarSize(this);
        mUsableScreenSize = getAppUsableScreenSize(this);

        mRootView = (View) findViewById(android.R.id.content);
        mRootView.post(new Runnable() {

            @Override
            public void run() {
                int[] hs = getStatusAndTitleBarHeight();
                int nfh = mRootView.getHeight();
                if (mBtmNavBarSize.y > 0)
                    nfh = mUsableScreenSize.y - mBtmNavBarSize.y - hs[1];
                ViewGroup.LayoutParams lp2 = mRootView.getLayoutParams();
                lp2.height = nfh;
                mRootView.setLayoutParams(lp2);
            }

        });

...
    public static Point getNavigationBarSize(Context context) {
        Point appUsableSize = getAppUsableScreenSize(context);
        Point realScreenSize = getRealScreenSize(context);

        // navigation bar on the right
        if (appUsableSize.x < realScreenSize.x) {
            return new Point(realScreenSize.x - appUsableSize.x, appUsableSize.y);
        }

        // navigation bar at the bottom
        if (appUsableSize.y < realScreenSize.y) {
            return new Point(appUsableSize.x, realScreenSize.y - appUsableSize.y);
        }

        // navigation bar is not present
        return new Point();
    }

    // get usable screen size (real minus bottom navigation bar)
    public static Point getAppUsableScreenSize(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        return size;
    }

    public static Point getRealScreenSize(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        Point size = new Point();

        if (Build.VERSION.SDK_INT >= 17) {
            display.getRealSize(size);
        } else if (Build.VERSION.SDK_INT >= 14) {
            try {
                size.x = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
                size.y = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {
            } catch (NoSuchMethodException e) {
            }
        }

        return size;
    }

    private int[] getStatusAndTitleBarHeight() {
        Rect rectangle = new Rect();
        Window window = getWindow();
        window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
        int statusBarHeight = rectangle.top;
        int contentViewTop =
                window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
        int titleBarHeight = contentViewTop - statusBarHeight;
        return new int[]{statusBarHeight, titleBarHeight};
    }
...
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接