2014-01-29 165 views
0

在我的應用程序中,我允許用戶通過單擊「添加更多聯繫人」來添加最多5 EditText視圖。但是當出現EditText視圖時,佈局垂直下降。所以我想爲EditText視圖放置一個ScrollView而不是Button。我嘗試但每次都失敗。有人可以請幫助。滾動視圖Android故障

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/mLayout" 
android:layout_width="match_parent" 
android:layout_height="237dp" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<ScrollView 
    android:layout_width="wrap_content" 
    android:layout_height="236dp" > 

    <EditText 
     android:id="@+id/edittext" 
     android:layout_width="298dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="16dp" 
     android:ems="10" 
     android:hint="@string/et_display" > 

     <requestFocus /> 
    </EditText> 
</ScrollView> 

<Button 
    android:id="@+id/bpickperson" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="169dp" 
    android:text="@string/b_pick" /> 

<Button 
    android:id="@+id/balert" 
    android:layout_width="288dp" 
    android:layout_height="wrap_content" 
    android:text="@string/b_alert" /> 

<Button 
    android:id="@+id/baddmorecontacts" 
    android:layout_width="288dp" 
    android:layout_height="wrap_content" 
    android:text="@string/b_addmorecontacts" /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <Button 
     android:id="@+id/b_facebook" 
     android:layout_width="144dp" 
     android:layout_height="fill_parent" 
     android:text="@string/b_facebook" /> 

    <Button 
     android:id="@+id/b_twitter" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/b_twitter" /> 
</LinearLayout> 

+0

你能不能給一些代碼,你編程添加'EditTexts',並通過'失敗everytime'究竟是什麼實際效果? –

+0

這裏沒有你的.xml中使用的滾動視圖。 – Umitk

+0

@bhanu:我沒有看到任何'ScrollView'。您需要爲您的LinearLayout添加一個子滾動視圖。 –

回答

1
// try this way 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/mLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 

     <EditText 
      android:id="@+id/edittext" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:ems="10" 
      android:hint="@string/et_display" > 
     </EditText> 
    </ScrollView> 

    <Button 
     android:id="@+id/bpickperson" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/b_pick" /> 

    <Button 
     android:id="@+id/balert" 
     android:layout_width="288dp" 
     android:layout_height="wrap_content" 
     android:text="@string/b_alert" /> 

    <Button 
     android:id="@+id/baddmorecontacts" 
     android:layout_width="288dp" 
     android:layout_height="wrap_content" 
     android:text="@string/b_addmorecontacts" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center"> 

     <Button 
      android:id="@+id/b_facebook" 
      android:layout_width="144dp" 
      android:layout_height="wrap_content" 
      android:text="@string/b_facebook" /> 

     <Button 
      android:id="@+id/b_twitter" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/b_twitter" /> 
    </LinearLayout> 
</LinearLayout> 
+0

正是我在尋找感謝先生 –

1

滾動型只能包含一個子視圖。現在它包含一個EditText,所以它不能包含任何更多的子視圖。你必須把你的EditTexts放到另一個佈局中,讓我們說一個LinearLayout並把這個LinearLayout放到ScrollView中。然後,將您的EditText添加到LinearLayout而不是ScrollView。

0

試試這個:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/mLayout" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    tools:context=".MainActivity" > 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id ="@+id/editlayout" 
    android:layout_marginTop="25dip" 
    > 
    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="50dp" > 
     <EditText 
      android:id="@+id/edittext" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:maxLines="5" 
      android:hint="et_display" > 

      <requestFocus /> 
     </EditText> 
    </ScrollView> 
</LinearLayout> 
    <Button 
     android:id="@+id/bpickperson" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:text="b_pick" /> 

    <Button 
     android:id="@+id/balert" 
     android:layout_width="288dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:text="b_alert" /> 

    <Button 
     android:id="@+id/baddmorecontacts" 
     android:layout_width="288dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:text="b_addmorecontacts" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dip" > 

     <Button 
      android:id="@+id/b_facebook" 
      android:layout_width="144dp" 
      android:layout_height="fill_parent" 
      android:text="b_facebook" /> 

     <Button 
      android:id="@+id/b_twitter" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="b_twitter" /> 
    </LinearLayout> 
    </LinearLayout>