【问题标题】:Fit Scroll View to Available Space in Android使滚动视图适合 Android 中的可用空间
【发布时间】:2016-07-25 22:58:49
【问题描述】:

我的 ScrollView 有问题。我想把它放在可用的空间里,但我不知道怎么做。

现在我在一个 LinearLayout 之前有我的 ScrollView 和我的 TextView:

        <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:fillViewport="true"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/previousimage"
        android:layout_marginTop="@dimen/margin">


        <TextView
            android:id="@+id/question_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margintop"
            android:gravity="center_horizontal"
            android:paddingBottom="@dimen/margin_five"
            android:scrollbars="vertical"
            android:textColor="@color/white"
            android:textSize="@dimen/common_textsize" />

    </ScrollView>
    <LinearLayout
        android:id="@+id/bottom_options"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/scrollView"
        android:layout_marginTop="@dimen/margin_small"
        android:gravity="center|bottom"
        android:orientation="vertical">

问题是,如果我的 TextView 中有这么多文本,LinearLayout 会下降并隐藏。我想滚动里面的文本。无需下推 LinearLayout。

我想为 ScrollView 自动占用这个空间以适应所有屏幕尺寸。

See the image Layaout

谢谢!

【问题讨论】:

标签: android android-layout layout


【解决方案1】:

在你的 ScrollView 之前定义 LinearLayout 并将 ScrollView 设置为 LinearLayout 之上,match_parent。像这样:

<LinearLayout
    android:id="@+id/bottom_options"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginTop="@dimen/margin_small"
    android:gravity="center|bottom"
    android:orientation="vertical">
    ...
</LinearLayout>

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:fillViewport="true"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/previousimage"
    android:layout_above="@+id/bottom_options"
    android:layout_marginTop="@dimen/margin">

    <TextView
        android:id="@+id/question_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/margintop"
        android:gravity="center_horizontal"
        android:paddingBottom="@dimen/margin_five"
        android:scrollbars="vertical"
        android:textColor="@color/white"
        android:textSize="@dimen/common_textsize" />

</ScrollView>

【讨论】:

  • 谢谢!这项工作完美!现在我的“TextView”和里面的文字有问题。它在底部显示文本,我想在 ScrollView 的中心显示它。
  • ScrollView 和 TextView 都有“android:layout_marginTop”。我认为它是重复的,所以你的文字会放在底部。另外,如果你想有文本“center_horizo​​ntal”,那么你需要改变,android:layout_width 为“match_parent”。
【解决方案2】:

ScrollView 应始终为match_parent(或至少小于可用空间),否则它只占用与其子项相同的空间,因此从它的角度来看,没有子项出现,因此无需滚动。

【讨论】:

  • 如果我在 ScrollView 中设置: android:layout_width="match_parent" android:layout_height="match_parent" 然后 LinearLayout 完全隐藏。
  • 你的线性布局是空的,所以它的高度为0。如果你需要在ScrollView下显示LinearLayout,你应该把它们嵌入到一个父布局中。
  • LinearLayout 已填充但我没有粘贴内容。现在我有了这个。 i.stack.imgur.com/zo04x.png 但是如果我有这么多的文字。 ScrollView 不起作用并将我的 LinearLayout 推到屏幕外。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-20
  • 1970-01-01
  • 2016-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多