在Horizo​​ntalScrollView中缩放ImageView

时间:2011-10-27 15:13:12

标签: android android-layout

我对Android开发很新,我一直遇到一些问题,让我的布局显示我想要的方式。 基本上我想要的是四个包含带标签的图像的侧滚视图。直到运行时才知道图像的数量。我希望横向滚动视图可以扩展到不同的大小。

现在,我的设置看起来像这样: main.xml中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff" android:id="@+id/build">
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_weight="1"
    android:layout_height="0dp" android:background="#ddd" android:id="@+id/ahsv" />
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_weight="1"
    android:layout_height="0dp" android:background="#ddd" android:id="@+id/ihsv" />
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_weight="2"
    android:layout_height="0dp" android:background="#ddd" android:id="@+id/mhsv" />
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_weight="3"
    android:layout_height="0dp" android:background="#ddd" android:id="@+id/rhsv" />

viewitem.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="fill_parent"
android:background="#fff">

<ImageView android:id="@+id/image" android:layout_height="fill_parent"
    android:layout_width="fill_parent" android:scaleType="center" />

<TextView android:id="@+id/title" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:textColor="#000"
    android:layout_gravity="center_horizontal|bottom" />

还有一个LinearLayout位于Horizo​​ntalScrollViews

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#fff"
android:id="@+id/horizll"/>

此时我的代码只是使用LayoutInflator实例化一堆这些视图项,将它们添加到LinearLayout(horizll),它被夸大并添加到Horizo​​ntalScrollView。 (所有这些都是在onCreate())

中完成的

代码几乎按预期工作。我有四个侧滚动列表,其高度与其权重相对应。但是,这些图像内部的图像卡在默认大小,换句话说,它们没有按照Horizo​​ntalScrollView的高度进行缩放。

我做错了什么?

提前感谢您的帮助

2 个答案:

答案 0 :(得分:1)

好吧,我终于明白了。我最后在SO上搜索了很多,并在using ViewTreeObserver上找到了一个问题。设置适当的VTO后,我能够指定FrameLayout的最小宽度和高度。有点hackish,但它完成了工作。

答案 1 :(得分:0)

尝试在ImageView中指定 scaleType 。例如,

android:scaleType="fitXY" 

将小图像拉伸到ImageView的边界。您可能还需要指定

android:adjustViewBounds="true"

保持宽高比。