gpt4 book ai didi

android - 如何为 ImageView 指定动态高度?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:10:17 24 4
gpt4 key购买 nike

我的显示器顶部有一个 ImageView,如下所示:

╔══════════════════════════════════════════════╗
║ ImageView ╔══════════════╗ ║
║ ║ ║ ║
║ ║ Actual image ║ ║
║ ║ ║ ║
║ ║ ║ ║
║ ║ ║ ║
║ ╚══════════════╝ ║
╚══════════════════════════════════════════════╝

在此之下,我有一系列的按钮和 TextViews..

我希望 ImageView 的高度根据屏幕的最大高度动态增加。我正在沿屏幕边缘对齐包含按钮的布局,我希望上面的 ImageView 占据屏幕的其余部分。

此外,由于 ImageView 包含一个居中图像,我还想设置最小高度,如果屏幕太小而无法显示 ImageView 和下面的按钮,我还想设置一个滚动条。

谢谢!

这可能吗?

最佳答案

如果您使用 android:layout_width,第一部分应该很简单。如果您仅为 ImageView(或其容器)设置 layout_width,它将扩展以尽可能多地占据父布局。例如,如果您想要一个 ImageView 占据整个屏幕其余部分的布局,您可以这样做:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:src="@drawable/icon"
android:scaleType="fitCenter" android:layout_weight="1" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- Put whatever you want in here -->
</LinearLayout>
</LinearLayout>

在这个例子中,我拉伸(stretch)图像以占据整个屏幕,但你也提出了“如果图像对于屏幕来说太大而我需要滚动怎么办?”的问题。在这种情况下,您需要做的就是将 ScrollView 添加到布局中。如果图片垂直过高,屏幕会自动滚动:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ImageView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:src="@drawable/huge"
android:scaleType="fitCenter" android:layout_weight="1" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- Insert your content here -->
</LinearLayout>
</LinearLayout>
</ScrollView>

需要注意的一件重要事情:如果您没有在 ScrollView 上将 android:fillViewport 设置为 true,那么太小的 ImageView 将不会填满整个屏幕。

关于android - 如何为 ImageView 指定动态高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2907786/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com