我该如何为ImageView指定动态高度?

10

我在屏幕顶部有一个ImageView,它看起来像下面这样:

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

以下是一系列按钮和文本视图。 我希望根据屏幕的最大高度动态增加 ImageView 的高度。我将包含按钮的布局对齐到屏幕底部边缘,我希望上面的 ImageView 占据其余屏幕空间。

另外,由于 ImageView 包含一个居中的图片,如果屏幕太小无法显示 ImageView 和下方的按钮,则还希望设置最小高度和滚动条。

谢谢!

这种做法可行吗?


14
赞成使用 ASCII 可视化的酷炫效果。+1 - womp
2个回答

5

如果您使用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>

在此示例中,我正在拉伸图像以占据整个屏幕,但您还需要考虑“如果图像太大而无法适应屏幕并且我需要滚动怎么办?” 在这种情况下,您只需将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将无法填满整个屏幕。


0

完全不是你要找的答案,但通过相对布局、滚动视图和类中的动态编码混合使用,这绝对是可能的。目前不在编程电脑附近,但模糊的答案是“可能?是的!”:)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接