Android 水平滚动视图显示与屏幕大小相同的视图

Android 水平滚动视图显示与屏幕大小相同的视图,android,scrollview,Android,Scrollview,我有一个带有3个ImageView的水平滚动视图。我需要与屏幕宽度相同的图像 你能帮我吗 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <HorizontalScrollV

我有一个带有3个ImageView的水平滚动视图。我需要与屏幕宽度相同的图像

你能帮我吗

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<HorizontalScrollView android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:fillViewport="true">
    <LinearLayout android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
        <ImageView android:id="@+id/image1"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_alignParentEnd="false"
                   android:src="@drawable/placeholder"/>
        <ImageView android:id="@+id/image2"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:src="@drawable/placeholder"/>
        <ImageView android:id="@+id/image3"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:src="@drawable/placeholder"/>
    </LinearLayout>
</HorizontalScrollView>


在每个图像视图和线性布局中更改为android:layout\u width=“match\u parent”,水平滚动视图

在每个图像视图和线性布局中更改为android:layout\u width=“match\u parent”,水平滚动视图

是的,我对“Stanislav Parkhomenko”也很满意。如果需要具有相同屏幕宽度的图像,请在HorizontalScrollView以及每个ImageView中使用android:layout\u width=“match\u parent”更改该值。就这些。

是的,我对《斯坦尼斯拉夫·帕克霍梅恩科》也很满意。如果需要具有相同屏幕宽度的图像,请在HorizontalScrollView以及每个ImageView中使用android:layout\u width=“match\u parent”更改该值。仅此而已。

因为屏幕宽度在不同的设备上会有所不同,所以实现这一点的方法之一是在运行时设置宽度。为了获得适当的屏幕宽度,我们可以使用
ViewTreeObserver
。以下是我从布局中派生的测试xml文件:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:id="@+id/ll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/image1"
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:background="@android:color/holo_red_light"/>

            <ImageView
                android:id="@+id/image2"
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:background="@android:color/holo_blue_bright"/>

            <ImageView
                android:id="@+id/image3"
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:background="@android:color/holo_green_dark"/>
        </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>

这应该可以做到。如果遇到任何问题,请告诉我。

由于屏幕宽度在不同的设备上会有所不同,因此实现这一点的方法之一是在运行时设置宽度。为了获得适当的屏幕宽度,我们可以使用
ViewTreeObserver
。以下是我从布局中派生的测试xml文件:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:id="@+id/ll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/image1"
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:background="@android:color/holo_red_light"/>

            <ImageView
                android:id="@+id/image2"
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:background="@android:color/holo_blue_bright"/>

            <ImageView
                android:id="@+id/image3"
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:background="@android:color/holo_green_dark"/>
        </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>

这应该可以做到。如果您遇到任何问题,请告诉我。

它是否会引入循环依赖关系,父级具有
android:layout\u width=“wrap\u content”
?不,如果您将其设置为与父级匹配,容器将以全宽包装。但由于要将其放在匹配父对象上,所以也可以将容器放在匹配父对象上,以避免它计算其宽度。@TyMarc但这些图像视图的父对象(linearlayout)是水平滚动视图的直接子对象,因此不能将其宽度设置为“匹配父对象”。不会发生任何变化。它不会引入循环依赖关系,父对象具有
android:layout\u width=“wrap\u content”
?不,如果将其设置为匹配父对象,容器将以全宽包装。但由于要将其放在匹配父对象上,所以也可以将容器放在匹配父对象上,以避免它计算其宽度。@TyMarc但这些图像视图的父对象(linearlayout)是水平滚动视图的直接子级,因此不能将其宽度设置为“匹配父级”。上面的更改更像是注释。“答案”部分仅用于[可能的]合法答案。上面看起来更像是一条评论。“答案”部分仅用于[可能的]合法答案。谢谢,图像视图居中,但它们不会占据整个屏幕的宽度,每侧都有一个边距。@DamienDuflot每侧?那不应该。也许这和图像有关?你能发布一个屏幕截图吗?谢谢,图像视图居中,但它们不占整个屏幕的宽度,每侧都有一个边距。@DamienDuflot每侧?那不应该。也许这和图像有关?你能发布一个截图吗?