2014-01-29 85 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>