【问题标题】:Android: Is there a way to get fill_parent to not shrink to fit the parent's parent?Android:有没有办法让 fill_parent 不缩小以适应父母的父母?
【发布时间】:2011-01-21 03:49:39
【问题描述】:

我的活动如下所示

| header           |
|------------------|
|  ViewGroup       |
|  that fills      |
|  the screen      |
|                  |
|     centered     |
|      button      |
|                  |
|------------------|
| footer           |

填充屏幕的 View Group 是 RelativeLayout 的子项,RelativeLayout 使用 fill_parent 和 layout_below 页眉和 layout_above 页脚填充屏幕。该RelativeLayout下的ViewGroup在运行时设置为与RelativeLayout相同的宽高。

之所以在运行时设置相同的高度是因为这个视图组可以用手指拖到左边(类似于Android主屏幕)如果我把它拖到左边就可以了。但是如果我把它拖到右边,我的 fill_parent 尺寸就会搞砸。

| header           |
|------------------|
|     '  ViewGroup |     ' 
|     '  that fills|     '
|     '  the screen|     '
|     '            |     '
|     '  centered  | <-- button not centered. It is only centered on the visible area, not the whole thing
|     '   button   |     '
|     '            |     '
|     '            |     '
|------------------|
| footer           |

我进行定位的方式是通过设置 ViewGroup 的左边距将其向右移动(或将其设置为负向左移动)

为什么我不能使用HorizontalScrollView

这是android的顶视图

     ' previous ' current  '   next   ' # Three panes that can be dragged to switch between
                |          | # The android screen bounderies

我计划让用户能够通过左右拖动屏幕来进入下一个/上一个。如果用户稍微拖动屏幕并且他/她拖得很慢

        ' previous ' current  '   next   ' 
                |          |

然后屏幕应该动画回到原位

     ' previous ' current  '   next   ' 
                |          |

如果用户做的很快

              ' previous ' current  '   next   ' 
                |          |

然后屏幕应该为其余部分设置动画

                ' previous ' current  '   next   ' 
                |          |

Previous 应该成为新的 current,我们应该有一个新的 previous

     ' previous ' current  '   next   ' 
                |          |

我使用了滚动视图,我会做这样的事情

     ' previous ' current  '   next   ' 
     |          |

除非我跳过滚动,否则现在没有空间容纳新的前一个

     ' previous ' current  '   next   ' 
                |          |

这对我来说听起来更糟。

所以我的选择是

  • AbsoluteLayout(已弃用/不要使用)
  • RelativeLayout.LayoutParams.setMargins(见下文。这就是我现在正在做的)
  • 写我自己的LayoutManager(听起来比它更容易)
  • 将像素绘制到屏幕上,而不是使用Views(DirectDraw 或 OpenGL)
  • HorizontalScrollView

【问题讨论】:

    标签: android layout dimensions


    【解决方案1】:

    如果您设置了一个正的左边距,那么您也应该设置一个负的右边距。这允许 ViewGroup 继续离开屏幕右侧的边缘。

    【讨论】:

    • 这似乎是一个非常臭的黑客。你为什么不扩展 Horizo​​ntalScrollView 以按照你想要的方式行事?
    • 我遇到了完全相同的问题。非常感谢您提供此解决方案!
    • 我几乎想使用 AbsoluteLayout。感谢您的巧妙解决方案!
    • @GeorgeBailey +1 哇,你震撼了!!,负边距右成功了!!
    • @Qberticus 这不是黑客,这就是框架应该如何工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 2012-12-24
    • 2015-10-23
    • 2021-06-05
    • 2015-03-26
    • 1970-01-01
    相关资源
    最近更新 更多