【问题标题】:Scaling ImageView inside of HorizontalScrollView在 Horizo​​ntalScrollView 内缩放 ImageView
【发布时间】:2011-10-31 23:13:13
【问题描述】:

我是 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" />

查看项目.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" />

Horizo​​ntalScrollViews 内部还有一个 LinearLayout

<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 的高度而缩放。

我做错了什么?

提前感谢您的帮助

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    好吧,我最终想通了。我最终在 SO 上环顾四周,并在using ViewTreeObserver 上找到了一个问题。设置适当的 VTO 后,我能够指定 FrameLayout 的最小宽度和高度。有点hackish,但它完成了工作。

    【讨论】:

    • 我无法让它以最小的宽度和高度工作,但我在 ImageView 上使用了 setLayoutParams(),这对我有用。
    【解决方案2】:

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

    android:scaleType="fitXY" 
    

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

    android:adjustViewBounds="true"
    

    保持纵横比。

    【讨论】:

    • 按照您的建议进行操作后,它现在将图像拉伸到文本的宽度。感谢您的快速回复
    猜你喜欢
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多