2010-05-13 124 views
3

在我的Android應用程序中,我有一個選項卡Activity。在其中一個標籤中,我有兩個TextView和兩個EditText s。第一個EditText只有一行,這沒關係。但是,我希望其他EditText,android:id="@+id/paste_code"佔用剩餘的空間,但無論我做什麼,它只會顯示一行。我不想手動設置行數,因爲適合屏幕的數量因您的設備而異。Android EditText不會佔用剩餘空間

下面是相關的代碼。它嵌套在標籤爲Activity的所有必要組件中。

<ScrollView 
    android:id="@+id/basicTab" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" > 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Paste title" 
      android:layout_weight="0" /> 
     <EditText 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:hint="@string/paste_title_hint" 
      android:id="@+id/paste_title" 
      android:lines="1" 
      android:gravity="top|left" 
      android:layout_weight="0" /> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="Paste text" 
      android:layout_weight="0" /> 
     <EditText 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:hint="@string/paste_hint" 
      android:id="@+id/paste_code" 
      android:gravity="top|left" 
      android:layout_weight="1" /> 
    </LinearLayout> 
</ScrollView> 

回答

9

由於接受的答案不能完全解決的情況下,這裏的人們來此同時尋找適當的解決辦法:

首先,羅曼蓋伊從Android開發團隊解決了這個好這個博客帖子:

http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/

從根本上講,ScrollView需要包含android:fillViewport="true"屬性。

如果事情不工作,一旦你做到了這一點,這裏有幾件事情要檢查:

  • ScrollView(如LinearLayout)需要佈局有layout_height="wrap_content"
  • 要擴大期(縣)應具有layout_height="wrap_content"
  • 視圖()你想擴展應該有layout_weight="1.0"或類似

如果您不想讓它縮小太多,請不要忘記在要擴展的視圖中設置minLines="3"或類似物。

+0

確實;謝謝你。我用另一個SO問題的鏈接評論了另一個答案,這個問題解決了我的問題。 – Jamie 2010-10-12 00:46:52

+0

工程就像一個魅力! – Damjan 2011-09-30 10:32:50

0

該問題似乎來自您使用ScrollView。我已經使用ScrollView作爲父容器測試了您的代碼,並且遇到了同樣的問題。但是,如果我用LinearLayout替換了ScrollView,則第二個EditText正確擴展以填充整個屏幕。問題一定是ScrollViews被設計成可以包裝成儘可能小的尺寸,而不管你在android:layout_height中放置了什麼設置。我嘗試了其他幾種佈局,例如使用layout_abovelayout_below的RelativeLayout,但這些隻影響其最大尺寸,而不影響其空白時的尺寸。不幸的是,這意味着我不知道如何解決你的問題......有沒有一種方法可以重新設計你的佈局使用除ScrollView之外的東西作爲父容器?

+0

是的,這個標籤可能沒有ScrollView,因爲(如果我沒記錯的話)EditTexts有自己的滾動條。我會玩一個LineaLayout。 謝謝。 – Jamie 2010-05-13 23:20:42

+0

此外,我剛剛發現http://stackoverflow.com/questions/2033296/android-scrollview-problem它肯定了你的android語句:layout_height沒有做任何事情,並提供另一個XML屬性,使其佔用屏幕的其餘部分。如果這不起作用,我會訴諸LinearLayout。 – Jamie 2010-05-13 23:25:54