【问题标题】:How can I specify a dynamic height for an ImageView?如何为 ImageView 指定动态高度?
【发布时间】:2010-05-25 19:16:58
【问题描述】:

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

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

在这下面,我有一系列的 Buttons 和 TextViews..

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

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

谢谢!

这可能吗?

【问题讨论】:

  • +1 用于 ascii 可视化效果。

标签: android android-layout


【解决方案1】:

如果你使用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 将不会填满整个屏幕。

【讨论】:

    【解决方案2】:

    完全不是您正在寻找的答案,但绝对有可能通过您的班级中的 relativelayout 滚动视图和动态编码的混合。目前不在编程计算机附近,但模糊的答案是“可能?是的!” :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-24
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      • 2017-08-20
      • 1970-01-01
      • 1970-01-01
      • 2013-06-11
      相关资源
      最近更新 更多