2011-06-01 90 views
0

我試圖創建一個簡單的佈局,但找不到propper辦法做到這一點。佈局應該有3個元素:頂部的TextView,中間的EditText(帶有滾動視圖)和底部的按鈕。 我做了這個代碼:Android的佈局問題

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

    <LinearLayout android:layout_weight="0.97" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:id="@+id/linearLayout18" 
    android:orientation="vertical"> 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"   
     android:text="Text:" 
     android:gravity="center_vertical"/> 

    <ScrollView android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <EditText 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/txtMessage" 
     android:text=""/> 

    </ScrollView> 
    </LinearLayout> 
    <LinearLayout 
    android:layout_weight="0.03" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:id="@+id/linearLayout1" 
    android:orientation="vertical"> 
     <Button 
     android:text="Next" 
     android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     />  
    </LinearLayout> 
</LinearLayout> 

但它給我的佈局是這樣的:Layout正如你所看到的問題是,EditText上不會採取所有可用的垂直空間(一路按鈕)。如何解決這個問題?

回答

1

就在您的ScrollView

+0

兩個相同(和正確)的答案。我選擇了你的 - 你先回答:)簡單而有效的解決方案,謝謝! :) – guest86 2011-06-01 12:07:57

+0

不貪婪但我配得上一個! :) – doNotCheckMyBlog 2011-06-01 12:11:37

1

嘗試的RelativeLayout的,而不是線性在這裏使用android:fillViewport="true"是samle代碼

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

    <RelativeLayout android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:id="@+id/linearLayout18" android:layout_alignParentTop="true"> 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"   
     android:text="Text:" 
     android:id="@+id/textMessage" 
     android:gravity="center_vertical" android:layout_alignParentTop="true"/> 

    <ScrollView android:layout_width="fill_parent" 
    android:layout_below="@+id/textMessage" android:layout_above="@+id/button1" android:scrollbars="horizontal|vertical" android:layout_height="fill_parent" android:fillViewport="true"> 

    <EditText 
     android:id="@+id/txtMessage" 
     android:text="" android:layout_height="fill_parent" android:layout_width="fill_parent"/> 

    </ScrollView> 


     <Button 
     android:text="Next" 
     android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true"/>  
    </RelativeLayout> 
</RelativeLayout> 
+0

那麼,加入只是android:fillViewport =「true」解決了問題,爲什麼我應該使用「相對佈局」? – guest86 2011-06-01 12:09:07

0

試試這個....這是完美的......我在我的Android工作室進行了測試.....這是相同的佈局,只要你想....

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

    <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Text:" 
      android:id="@+id/textview1" 
      android:layout_alignParentTop="true"/> 


     <ScrollView android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:fillViewport="true" 
      android:layout_above="@id/btn" 
      android:layout_below="@id/textview1"> 

      <EditText 
       android:layout_width="fill_parent" 
       android:layout_height="match_parent" 
       android:id="@+id/txtMessage" 
       android:text=""/> 

     </ScrollView> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/txtMessage" 
     android:id="@+id/btn" 
     android:text="Next" 
     android:layout_alignParentBottom="true"/> 

    </RelativeLayout>