Android: ScrollView 中的两个图像

标签 android xml android-layout

我希望能够向用户显示 2 张图像,但是我希望这些图像(水平)填满屏幕,然后能够向下滚动到下一张图像,这可能吗?我一直在搞乱不同的 XML 布局,似乎找不到解决方案。 当我在 ScrollView 中放置一个 ImageView 时,它没有填满屏幕,我怎样才能在其中获得 2 个但只有 1 个可见?

 ?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/building_block"
    android:layout_margin="2dp">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:id="@+id/scrollView"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true">

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:adjustViewBounds="true"
            android:gravity="center"
            android:scaleType="fitXY"
            android:src="@drawable/ic_ntm"
            android:id="@+id/imageView15" />

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:adjustViewBounds="true"
            android:gravity="center"
            android:scaleType="fitXY"
            android:src="@drawable/ic_ntm"
            android:id="@+id/imageView17"
            android:layout_marginTop="44dp" />

    </ScrollView>


</RelativeLayout>

这就是我想要的,但可能吗?一张图片在另一张图片下方,每个图片都填满屏幕并在 ScrollView 中?

最佳答案

您不能向 ScrollView(或 Horizo​​ntalScrolView)添加超过一个子项。

您可以使用子布局修复它以同时拥有这两个元素。

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:id="@+id/scrollView"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true">
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="true"
        android:layout_weight="1"
        android:gravity="center"
        android:scaleType="fitXY"
        android:src="@drawable/ic_ntm"
        android:id="@+id/imageView15" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="true"
        android:gravity="center"
        android:scaleType="fitXY"
        android:layout_weight="1"
        android:src="@drawable/ic_ntm"
        android:id="@+id/imageView17"
        android:layout_marginTop="44dp" />
     </LinearLayout>
</ScrollView>

还要注意 android:layout_weight="1",所以每个 ImageView 可以占 View 的一半,因为如果两者都是 fill_parent,则只有第一个会显示。

关于Android: ScrollView 中的两个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31598360/

相关文章:

android - 移除 android 中 Tabs 布局的阴影。 API >= 21

android - Robolectric 与 android 中的数据库

android - Html.fromHtml() 删除回车符 "&#xD"、 "\n"

android - 为 Android 应用程序下载 layout.xml 并使用

java - XSLT/Java : ERROR: 'Cannot find external method ' max' (must be public). '

Android - 如果没有互联网连接则显示一条消息并继续检查

c# - Xamarin 商店信息

android - 我如何使用 Adob​​e Dreamweaver 6 和 PhoneGap 编写数据库驱动的 iOS、Android 等应用程序?

android - 如何在android中获取DialogResult?

android - View.GONE 不适用于 "Constraint.Group"特定子项