繁体   English   中英

在Horizo​​ntalScrollView内缩放ImageView

[英]Scaling ImageView inside of HorizontalScrollView

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

在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实例化了一堆这些viewitem,将它们添加到LinearLayout(水平)中,然后将其放大并添加到Horizo​​ntalScrollView。 (所有这些操作都在onCreate()中完成)

该代码几乎按预期工作。 我有四个侧面滚动列表,它们的高度与它们的重量相对应。 但是,其中的图像将保留其默认大小,换句话说,它们不会随Horizo​​ntalScrollView的高度缩放。

我究竟做错了什么?

先谢谢您的帮助

好吧,我最终想通了。 我最终在SO上寻找了更多东西,并发现了有关使用ViewTreeObserver的问题。 设置了适当的VTO之后,我可以指定FrameLayout的最小宽度和高度。 有点骇人听闻,但可以完成工作。

尝试在ImageView中指定scaleType 例如,

android:scaleType="fitXY" 

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

android:adjustViewBounds="true"

保持宽高比。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM