如何为ImageView指定动态高度?

kef*_*efs 10 android android-layout

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

????????????????????????????????????????????????
? ImageView    ????????????????                ?
?              ?              ?                ?
?              ? Actual image ?                ?
?              ?              ?                ?
?              ?              ?                ?
?              ?              ?                ?
?              ????????????????                ?
????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)

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

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

此外,由于ImageView包含居中图像,我还想设置最小高度,如果屏幕太小则无法显示图像视图和下面的按钮.

谢谢!

这可能吗?

Dan*_*Lew 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>
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我正在拉伸图像以占据整个屏幕,但是你也提出了"如果图像对于屏幕太大而我需要滚动怎么办?"的问题.在这种情况下,您需要做的就是将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>
Run Code Online (Sandbox Code Playgroud)

需要注意的一件重要事情是:如果android:fillViewportScrollView上没有设置为true,那么太小的ImageView将无法填满整个屏幕.