【问题标题】:Place a block at the center of a ScrollView在 ScrollView 的中心放置一个块
【发布时间】:2012-08-24 01:46:55
【问题描述】:

我有一个 ScrollView,其中包含一个子项(由 LinearLayout 包装的元素)。有时这个孩子的高度是视口高度的一半,有时更大。

这里的布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
    android:paddingTop="10dp"
    android:background="@color/some_background">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:background="@drawable/fond_consult"
        android:orientation="vertical"
        android:weightSum="2">

        ... Some TextViews and ImageViews...

    </LinearLayout>
</ScrollView>

当屏幕尺寸和密度较低时,子项可以正确显示在 ScrollView 中,但是当在 HDPI 和 XHDPI 等更大的屏幕上渲染此 Activity 时,内容会放在 ScrollView 的顶部。

有没有办法为 ScrollView 的唯一孩子设置重力,或者如果设备屏幕大于孩子的测量高度,我是否必须删除 scrollView?

【问题讨论】:

  • 尝试将android:layout_gravity="center" 添加到滚动视图,其layout_widthlayout_heightwrap_content
  • 非常感谢,成功了!您能否将此作为答案发布,以便我接受?

标签: android android-layout scrollview layout-gravity


【解决方案1】:

您可以尝试将android:layout_gravity="center" 添加到ScrollView,并将其layout_widthlayout_height 添加为wrap_content。由于 ScrollView 自行管理其子布局的高度和宽度。

【讨论】:

  • 大声笑我搜索了多个stackoverflow-questions来寻找这个问题,没有人提到真正的解决方案,只是它的原因。然后就是你... :)
【解决方案2】:

在正常情况下,ScrollView 只会占用最少的空间,所以根本不会出现垂直对齐的问题(垂直滚动的情况下)。您可以浏览http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/ 中的文章。它可能会帮助你。

【讨论】:

  • 带有 layout_height="match_parent" 的 ScrollView 占据了整个空间,但 Lalit Poptani 的评论是我需要的技巧。不过,谢谢你的链接!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-15
  • 2018-09-27
  • 1970-01-01
  • 2019-12-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多