2013-12-18 4 views
5

Ich habe eine abc.xml mit der folgenden Struktur.Android: Hinzufügen von Ansicht dynamisch in einem verschachtelten Layout

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
<RelativeView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <LinearLayout 
    android:id="@+id/linear" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    > 
    </LinearLayout> 
</RelativeLayout> 
</ScrollView> 

Ich möchte Textansichten dynamisch zum linearen Layout hinzufügen. Unten ist mein Code. Ich bekomme keine Fehler, aber ich bekomme nicht die gewünschten Ergebnisse.

LayoutInflater Inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view = Inflater.inflate(R.layout.abc, null); 

LinearLayout layout = (LinearLayout) view.findViewById(R.id.linear); 

     TextView Tag = new TextView(getActivity()); 
     Tag.setText("textString"); 
     Tag.setBackgroundResource(R.color.bg_color); 
     Tag.setTextAppearance(getActivity(), R.style.SmallFont); 
     layout.addView(Tag); 
+0

Warum verwenden Sie RelativeLayout? –

+0

Ich habe andere Ansichten im relativen Layout wie Textviews und Buttonviews – Shah

Antwort

6

Ihre xml

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <LinearLayout 
    android:id="@+id/linear" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    > 
    </LinearLayout> 
</LinearLayout> 
</ScrollView> 

und der Java-Code Ihren Text Ansicht hinzuzufügen sein sollte, ist

LinearLayout layout = (LinearLayout) view.findViewById(R.id.linear); 
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
    LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT); 
TextView Tag = new TextView(getActivity()); 
tag.setLayoutParams(params); 
Tag.setText("textString"); 
Tag.setBackgroundResource(R.color.bg_color); 
Tag.setTextAppearance(getActivity(), R.style.SmallFont); 
layout.addView(Tag); 
0

Ich weiß nicht, Ihnen die Antwort noch nicht gefunden haben, aber ich hatte einfach stolperte über dieses Problem und ich fand 1 Weg. Ich glaube, Sie brauchen das Layout von einem Bit wie folgt anzupassen:

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

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

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

      <LinearLayout 
       android:id="@+id/linear" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" > 
      </LinearLayout> 
     </LinearLayout> 

    </RelativeView> 

</ScrollView> 
0

Sie müssen geben,

android:layout_orientation="either horizontal or vertical" 

zum Linear-Layout mit ID: linear.