2010-12-01 18 views
2

我需要有一個比屏幕大的用戶窗體。該佈局由一個固定的「標題區域」,表單(可能在scrollview中)和底部的按鈕組成。我創建了三個佈局,中間佈局是一個帶垂直方向LinearLayout的ScrollView。不幸的是,表單將按鈕從屏幕上推出?!如何佈置在Android SDK中滾動的表單?

有沒有一個簡單的例子來做到這一點?

我想我可以使用ListView,但我會認爲我的ScrollView更容易管理。這是我的佈局......

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

    <!-- Title Area on top --> 
    <LinearLayout 
    android:id="@+id/layout_form" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    > 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
    > 
     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="0dip" 
      android:layout_marginBottom="0dip" 
      android:paddingTop="20dip" 
      android:paddingBottom="20dip" 
      android:paddingLeft="5dip" 
      android:paddingRight="10dip" 
      android:src="@drawable/logo" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="2" 
      android:layout_marginTop="0dip" 
      android:layout_marginBottom="0dip" 
      android:layout_marginLeft="4dip" 
      android:layout_marginRight="4dip" 
      android:text="@string/title_create" 
      /> 
    </LinearLayout> 

    <!-- Form entry on top --> 

     <ScrollView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:fillViewport="true" 
      android:clipChildren="true" 
      > 
      <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
      > 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginLeft="8dip" 
      android:layout_marginRight="8dip" 
      android:text="@string/label_field_one" 
      /> 
     <EditText 
      android:id="@+id/edit_field_one" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:singleLine="true" 
      android:layout_marginLeft="10dip" 
      android:layout_marginRight="10dip" 
      android:hint="@string/hint_field_one" 
      /> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginLeft="8dip" 
      android:layout_marginRight="8dip" 
      android:text="@string/label_field_two" 
      /> 
     <EditText 
      android:id="@+id/edit_field_two" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:singleLine="true" 
      android:layout_marginLeft="10dip" 
      android:layout_marginRight="10dip" 
      android:hint="@string/hint_field_two" 
      /> 

      <!-- Repeat label-text and edit-text until done ... --> 

      </LinearLayout> 
      </ScrollView> 


    <!-- "spring" between form and wizard buttons. --> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
    /> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="left" 
     android:layout_weight="2" 
     > 
     <Button 
      android:id="@+id/btn_submit" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" 
      android:layout_weight="1" 
      android:text="@string/button_submit" 
     /> 
    </LinearLayout> 
     </LinearLayout> 


</LinearLayout> 

回答

3

如果我理解正確的話,這是東西是帶來了相當普遍 - 具有固定的頁眉和頁腳,並填寫與任何中間(在你的情況下,滾動型)。那是你在找什麼?如果是的話,你會想嘗試像下面的東西:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <LinearLayout 
     android:id="@+id/header" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     > 
     //.....stuff here 
    </LinearLayout> 
    <LinearLayout 
     android:id="@+id/footer" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     > 
     //.....stuff here 
    </LinearLayout> 
    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_above="@id/footer" 
     android:layout_below="@id/header" 
     > 
     //......stuff here 
    </ScrollView> 
</RelativeLayout> 

基本上,你定義了一個頭,給它一個固定的高度(在這種情況下WRAP_CONTENT),並將其設置對齊到父的頂部(RelativeLayout)。然後,對頁腳執行相同的操作,將其對齊到父項的底部。對於您的情況,您只需將LinearLayout的Button替換爲您的情況,或者只需將按鈕放置在LinearLayout內即可。如果沒有額外的LinearLayout,膨脹佈局可能稍微快一點。但無論如何,接下來,定義你的表單,滾動區域。將高度設置爲fill_parent,並告訴它在頁腳上方,並在頁眉下方。這將導致它佔用頁眉和頁腳之間的所有剩餘空間。

+0

是的,我正在尋找標題(指導)/頁腳(可操作)與中間滾動的窗體。我現在將編碼,並讓你知道這是否適用於我的佈局。 – mobibob 2010-12-01 22:59:01