2010-05-25 54 views
10

我有我的显示器的顶部,看起来像下面的ImageView的:如何为ImageView指定动态高度?

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

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

我想的ImageView高度动态根据屏幕的最大高度而增加。我沿着屏幕边缘的底部对齐包含按钮的布局,并且我希望上述ImageView占用屏幕的其余部分。另外,由于ImageView包含一个居中图像,我还想设置最小高度,如果屏幕太小而不能显示下面的图像视图和按钮,则需要一个滚动条。

谢谢!

这可能吗?

+13

+1 ascii visual awesomeness。 – womp 2010-05-25 19:20:07

回答

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> 

一个重要的一点要注意:如果你没有android:fillViewport设置为true的滚动型,太小会被填满ImageViews整个屏幕。

0

完全不是你正在寻找的答案,但它绝对可以通过你的课堂上的相关布局滚动视图和动态编码的混合。目前不在编程计算机附近,但模糊的答案是“可能的,是的!” :)