2012-03-14 174 views
1

我正在开发2.2版本的Android应用程序。Android应用程序布局

我的应用程序必须甲肝这样的结构:

[ Spinner here (fixed height) ]

[ ListView (not-fixed height) ]

[ ImageView (fixed-height) ]

我只使用纵向。

我对它使用linearLayout。我如何计算listView高度以在屏幕顶部显示微调,底部imageview和listview覆盖所有空闲空间,但不会将其他人从视野中推开。

对于很多屏幕分辨率来说,它会很酷。

谢谢

回答

1

使用相对布局来实现你所需要的。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <Spinner 
     android:id="@+id/spinner1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" /> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:src="@drawable/icon" /> 

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/spinner1" 
     android:layout_above="@+id/imageView1" /> 

</RelativeLayout> 

通过这样的列表视图,使其微调和ImageView的屏幕

1

您需要使用android:layout_weight属性的高度可变的项目,所以它填满可用空间。

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 

<Spinner 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 

<ListView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    /> 

<ImageView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 

</LinearLayout> 
+0

ListView是否仍然覆盖这个例子中的图片?它似乎是这样。 – prolink007 2012-03-14 20:22:52

+0

试试吧,你会看到的。布局使用layout_weight,它指定视图之间的相对比率。 0表示固定大小,更大(例如,我使用的1)允许它伸展。因此,如果第一个和第三个视图是固定尺寸,则在其高度上使用wrap_content,或设置明确的高度,例如, 30dp – 2012-03-15 09:13:35

+0

我只是检查它没有在布局中的任何东西,它覆盖,但我会尝试添加的东西到布局,看看。 – prolink007 2012-03-15 13:15:57

0

这里就适合之间将调整其大小是另一种方式来做到这一点,利用weights。这可能是最简单的方法。

您可以将weight放入您希望占用的屏幕的百分比。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Spinner 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="10"/> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="80"></ListView> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="10"/> 


</LinearLayout> 

编辑: 现在我想想,这将“修复”所有的部件的尺寸。他们将以屏幕百分比“固定”。这将与其他屏幕尺寸很好地扩展,但你确实说你想要ListView不被修复。