【问题标题】:How to move a layout content outside the screen android如何将布局内容移动到屏幕android之外
【发布时间】:2013-01-05 18:07:30
【问题描述】:

我需要将屏幕内容向左移动,以便为右侧的幻灯片菜单腾出空间

代码如下:

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

但我得到的是:

内容只是调整大小以适应屏幕,我怎样才能做到这一点? 顺便说一句,我无法修改内容布局,它是带有表面视图的相对布局,但它应该适用于任何布局,因为我修改了作为顶视图容器的 DecorView

【问题讨论】:

    标签: android layout view


    【解决方案1】:

    使用父级作为衬垫布局,为左右布局视图设置权​​重。 将您的左视图放在需要滚动到屏幕外的水平滚动视图中。

    【讨论】:

      【解决方案2】:

      要移动“屏幕”,您需要在视图上调用 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 我昨天用一个例子更新了我的答案,这有助于回答你的问题吗?
      猜你喜欢
      • 2011-09-09
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      • 2018-04-10
      • 1970-01-01
      • 2019-03-10
      • 2017-03-19
      • 1970-01-01
      相关资源
      最近更新 更多