如何将Android布局内容移出屏幕?

6
我需要将屏幕内容向左移动,以便为右侧的滑动菜单腾出空间。 在此输入图片描述 以下是代码:
// modify content layout params
    try {
        content = ((LinearLayout) act.findViewById(android.R.id.content)
                .getParent());
    } catch (ClassCastException e) {
        /*
         * When there is no title bar
         * (android:theme="@android:style/Theme.NoTitleBar"), the
         * android.R.id.content FrameLayout is directly attached to the
         * DecorView, without the intermediate LinearLayout that holds the
         * titlebar plus content.
         */
        content = (FrameLayout) act.findViewById(android.R.id.content);
    }
FrameLayout.LayoutParams pr = (android.widget.FrameLayout.LayoutParams) content
                    .getLayoutParams();
            pr.rightMargin = menuSize;
            content.setLayoutParams(pr);
// add the slide menu to parent
    parent = (FrameLayout) content.getParent();

    try {
        parent = (FrameLayout) content.getParent();
    } catch (ClassCastException e) {
        /*
         * Most probably a LinearLayout, at least on Galaxy S3.
         */
        LinearLayout realParent = (LinearLayout) content.getParent();
        parent = new FrameLayout(act);
        realParent.addView(parent, 0); // add FrameLayout to real parent of
                                        // content
        realParent.removeView(content); // remove content from real parent
        parent.addView(content); // add content to FrameLayout
    }

    LayoutInflater inflater = (LayoutInflater) act
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    menu = inflater.inflate(R.layout.slidemenu, null);

    FrameLayout.LayoutParams lays = new FrameLayout.LayoutParams(menuSize,
            FrameLayout.LayoutParams.MATCH_PARENT, Gravity.RIGHT);
    lays.setMargins(0, statusHeight, 0, 0);
    menu.setLayoutParams(lays);
    parent.addView(menu);

但我得到的只是:
enter image description here 内容只是被调整大小以适应屏幕,我该怎么做才能让它工作呢? 顺便说一下,我无法修改内容布局,因为它是一个相对布局,带有一个SurfaceView,但它应该适用于任何布局,因为我修改的DecorView是顶部视图容器。
2个回答

5
要移动“屏幕”,您需要调用视图上的getLayoutParams()方法,根据需要进行修改,然后在视图上调用setLayoutParams()方法。

但是,如果想了解如何实现滑入菜单的教程,请参见此处:-

http://android.cyrilmottier.com/?p=658

为了提供更多帮助,以下是一个布局,可以实现我认为你想要的效果:-
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
 >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue" >


</LinearLayout>

<LinearLayout
    android:layout_width="200dip"
    android:layout_height="match_parent"
    android:layout_marginLeft="-100dip"
    android:background="@color/grey_divider"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

这将导致第二个LinearLayout出现在屏幕左侧的50%处。请注意,您要移动的LinearLayout需要具有绝对宽度。但是,您可以在xml中将其定义为FILL_PARENT,然后在第一次将边距设置为负值时,在代码中获取宽度并将其设置为绝对值。
希望这能帮到您。

我在这里执行以下操作: FrameLayout.LayoutParams pr = (android.widget.FrameLayout.LayoutParams) content .getLayoutParams(); pr.rightMargin = menuSize; content.setLayoutParams(pr); 菜单和动画都准备好了,我只需要修复这个问题。 - user924941
@user924941,我昨天更新了我的答案,并提供了一个示例,这有助于回答你的问题吗? - Ryan

1
使用父布局作为线性布局,在左右布局视图中设置权重。 将左侧视图放在需要滚动出屏幕的HorizontalScrollView中。

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