【问题标题】:How to make 2 images views each be sized to cover screen inside a linear layout inside a horizontal scrollview如何使 2 个图像视图的大小分别调整为覆盖水平滚动视图内的线性布局内的屏幕
【发布时间】:2013-08-17 10:56:00
【问题描述】:

我试图在水平滚动视图内的线性布局内获得 2 个(或更多)图像视图,这样每个图像都被缩放以适应屏幕高度而不扭曲图像,即一个图像在屏幕上显示尽可能大,滚动查看下一个。

添加第二张图片后,我会得到 2 张彼此相邻的小图片(两张图片的尺寸均为 217 * 300 像素)。目前我的 activity_main.xml 如下所示..

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

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            >
            <ImageView
                android:id="@+id/image1"
                android:src="@drawable/luke"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitCenter"
                />
            <ImageView
                android:id="@+id/image2"
                android:src="@drawable/luke"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitCenter"
                />


        </LinearLayout>
    </HorizontalScrollView>


</FrameLayout>

我在布局上尝试了 match_parent、fill_parent、wrap_content 的各种组合,图像视图上的所有 scaleTypes 并花了一天时间浏览网络寻找与我想要的类似的示例,但没有运气。

【问题讨论】:

    标签: android imageview android-linearlayout fullscreen horizontalscrollview


    【解决方案1】:

    我会说设置这两个东西:

         <ImageView
              android:layout_height = "match_parent"
              android:layout_width = "10dp" />
    

    这是棘手的部分。运行时获取屏幕宽度,设置为imageView。

    【讨论】:

    • 嗯好的。我明天上班时试试这个。您选择 10dp 有什么特别的原因吗?为 dp 值选择什么实际上是否重要,或者它只是一个占位符,直到它在运行时在活动代码中设置?
    • 谢谢,这对我有用。我使用this 获取屏幕尺寸,并使用this 作为设置活动中图像视图宽度的指南
    【解决方案2】:

    我倾向于使用带有自定义页面适配器的 viewpager 来显示您的缩放图像视图,而不是尝试以您当前的方式进行。

    视图分页器/适配器将为您处理视图回收、页面捕捉等。

    【讨论】:

    • 明天上班的时候看看viewpager是怎么工作的
    【解决方案3】:

    如何使用

    android:width="0"
    android:weight ="1"
    android:height="match_parent"
    

    在图像视图中

    和 match_parent 在你的布局中的其他位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多