如何在屏幕外移动布局内容android

use*_*941 5 layout android view

我需要将屏幕内容移到左侧,这样我就可以为右侧的幻灯片菜单腾出空间 在此输入图像描述

这是代码:

// 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);
Run Code Online (Sandbox Code Playgroud)

但我得到的是: 在此输入图像描述 内容只是调整大小以适应屏幕,我该如何使这项工作?顺便说一句,我不能修改内容布局它的相对布局与surfaceview,但它应该适用于任何布局,因为我修改DecorView是顶视图容器

Rya*_*yan 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>
Run Code Online (Sandbox Code Playgroud)

这将导致第二个 LinearLayout 出现在屏幕左侧 50% 处。仅供参考,您正在移动的 LinearLayout 需要一个绝对宽度。但是您可以在 xml 中将其定义为 FILL_PARENT,然后在第一次将边距设置为负值时获取宽度并将其设置为代码中的绝对值。

希望这可以帮助。