在 HorizontalScrollView 内部缩放 ImageView
我对 Android 开发还很陌生,在让我的布局以我想要的方式显示时遇到了一些问题。 基本上我想要的是四个包含带有标签的图像的横向滚动视图。图像的数量直到运行时才知道。我希望横向滚动视图可以缩放到不同的尺寸。
现在,我的设置看起来像这样: 主要.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" />
HorizontalScrollViews 内部还有一个 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),然后膨胀并添加到 HorizontalScrollView。 (所有这些都是在 onCreate() 中完成的)
代码几乎按预期工作。我有四个横向滚动列表,其高度与其重量相对应。但是,其中的图像停留在默认大小,换句话说,它们不随 HorizontalScrollView 的高度缩放。
我做错了什么?
预先感谢您的帮助
I'm pretty new to android development, and I've been having some problems getting my layout to show the way I want it to.
Basically what I want is four side-scrolling views containing an images with labels. The number of images isn't known until run-time. I want the side-scrolling views to be scalable to different sizes.
Right now, the setup I have looks something like this:
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" />
There's also a LinearLayout that goes inside of the HorizontalScrollViews
<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"/>
At this point my code just instantiates a bunch of these viewitems using LayoutInflator, adds them to the LinearLayout (horizll), that was inflated and added to the HorizontalScrollView. (all of this is done in onCreate())
The code works almost as intended. I have four side-scrolling lists that have heights corresponding to their weights. However, the images inside of these are stuck at their default size, in other words, they are not scaling with the height of the HorizontalScrollView.
What am I doing wrong?
Thanks in advance for the help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我最终想通了。我最终在 SO 上进行了更多查找,并在 使用 ViewTreeObserver。设置适当的 VTO 后,我能够指定 FrameLayout 的最小宽度和高度。有点黑客,但它完成了工作。
Well I eventually figured it out. I ended up looking around a lot more on SO, and found a question on using ViewTreeObserver. After setting up an appropriate VTO, I was able to specify the minimum width and height of the FrameLayout. A bit hackish, but it gets the job done.
尝试在 ImageView 中指定 scaleType 。例如,
将小图像拉伸到 ImageView 的边界。您可能还需要指定
保持宽高比。
try specifying scaleType in ImageView. For instance,
will stretch small image up to ImageView's boundaries. You might also need to specify
to keep aspect ratio.