2013-08-23 4 views
0

У меня возникает проблема, когда мне нужно создавать динамические представления один под другим внутри прокрутки. Метки должны быть сгенерированы после нажатия кнопки.динамическое создание вида макета внутри полосы прокрутки

для первой кнопки мыши, она работает нормально, но после второй мыши он терпит неудачу с ошибкой «java.lang.IllegalStateException: ScrollView может принять только один прямой ребенок»

может кто-нибудь помочь мне получить это неподвижное ,

Моя основная раскладка деятельность, где Scrollview является activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" 
     tools:context=".MainActivity" android:layout_width="match_parent" android:layout_height="match_parent" 
     android:id="@+id/parentRL"> 
    <Spinner android:id="@+id/spinState" android:layout_width="wrap_content" android:layout_height="wrap_content"/> 
    <Spinner android:id="@+id/spinCity" android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:layout_below="@id/spinState"/> 
    <TextView android:id="@+id/cityCount" android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/spinCity" android:layout_below="@id/spinState" android:layout_marginLeft="5dp" 
     android:textIsSelectable="false"/> 
    <Button android:id="@+id/showAddress" android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/spinState" android:onClick="populateAddress" android:text="Search"/> 
    <ScrollView android:id="@+id/scView" android:layout_width="match_parent" android:layout_height="wrap_content" 
     android:layout_below="@id/spinCity"> 
    </ScrollView> 
</RelativeLayout> 

И раскладка мнение, что необходимо приложить с Scrollview является address_layout.xml:

<?xml version="1.0" encoding="utf-8"?> 
<TextView android:id="@+id/center" android:layout_width="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_height="wrap_content" android:text="@string/hello_world" android:layout_margin="10dp" 
     android:textColor="#FF0000" android:textSize="30dp" 
/> 

В основной деятельности, внутри прослушиватель кнопки:

LayoutInflater inflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View myView = inflater.inflate(R.layout.address_layout, null); 
TextView _c_ = (TextView)(myView.findViewById(R.id.center)); 
_c_.setVisibility(View.VISIBLE); 
_c_.setText(my_random_number); 
View insertPoint = findViewById(R.id.scView); 
((ViewGroup) insertPoint).addView(myView, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 

ответ

0

андроид scrollview поддерживает только ребенка, поэтому t hat он бросает java.lang.IllegalStateException. Вы должны добавить один слой LinearLayout или Relative в scrollview, а затем добавить дочерние элементы в этот макет.

<ScrollView android:id="@+id/scView" android:layout_width="match_parent" android:layout_height="wrap_content" 
     android:layout_below="@id/spinCity"> 
<LinearLayout android:id="@+id/layout" 
android:layout_width="match_parent" android:layout_height="wrap_content"> 
    </LinearLayout> 
    </ScrollView> 
Смежные вопросы