1

Ich versuche TextView zu dem verschachtelten linearenLayout in fragmentin onCreateView hinzuzufügen, ich weiß nicht, ob dies der richtige Ansatz ist oder nicht, neu für Android! Vielen Dank im VorausHinzufügen von Ansichten dynamisch im linearen Layout in Fragment

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_screen_slide_page, container, false); 
    List<LifeBerrysXmlParser.Item> items = (ArrayList<LifeBerrysXmlParser.Item>) getArguments().getSerializable("List"); 
    TextView mainHeading = (TextView) rootView.findViewById(R.id.mainHeading); 
    ImageView mainImage = (ImageView) rootView.findViewById(R.id.articleMainImage); 
    LinearLayout articleLayout = (LinearLayout) rootView.findViewById(R.id.articleLayout); 
    TextView tev = new TextView(getActivity()); 
    tev.setText("Hello.............."); 
    articleLayout.addView(tev); 
    LifeBerrysXmlParser.Item item = items.get(getArguments().getInt("position")); 
    String articleMainHeading = item.mainHeading; 
    String articlemainImage = item.mainImage; 
    mainHeading.setText(articleMainHeading); 
    if (!articlemainImage.isEmpty()) { 
     Picasso.with(getContext()).load(articlemainImage).into(mainImage); 
    } 
    return rootView; 
} 

kann jemand bitte helfen Sie mir, was ich falsch mache?

hier ist mein xml für das Fragment

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="true" 
     android:id="@+id/content" 
     android:padding="1dp" 
     android:layout_weight="1" 
     android:background="#000000" 
     android:layout_gravity="center"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:background="#ffffff" 
      android:scrollIndicators="right"> 
      <RelativeLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
       <ImageView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/articleMainImage" 
        android:maxHeight="300dp"/> 
       <TextView 
        style="?android:textAppearanceMedium" 
        android:layout_width="match_parent" 
        android:layout_height="?android:attr/listPreferredItemHeightLarge" 
        android:layout_weight="1" 
        android:lineSpacingMultiplier="1.2" 
        android:id="@+id/mainHeading" 
        android:textColor="#ff6435" 
        android:clickable="false" 
        android:layout_gravity="center" 
        android:layout_centerInParent="true"/> 
      </RelativeLayout> 
      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/articleLayout" 
       android:orientation="horizontal"> 
       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="Read More" 
        android:textSize="15dp" 
        android:textStyle="bold" 
        android:layout_gravity="center" 
        android:clickable="true" 
        android:focusable="false" 
        android:gravity="center" 
        android:onClick="readMore" 
        android:padding="1dp" 
        android:textColor="@android:color/holo_green_light" /> 
      </LinearLayout> 
     </LinearLayout> 
</LinearLayout> 
+0

Erhalten Sie einige Fehler, oder 'TextView' wird nur nicht angezeigt? – RediOne1

+0

Keine Fehler, nur TextView ist nicht sichtbar –

+0

Überprüfen Sie meine Antwort – RediOne1

Antwort

3

Sie haben Fehler in Ihrem Artikel-Layout. Ihr Artikellayout hat eine horizontale Ausrichtung und sein untergeordnetes TextView hat die Breite . Wenn Sie also ein neues Kind hinzufügen, befindet es sich außerhalb des Bildschirms. Ändern Sie die Ausrichtung zu vertikal:

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/articleLayout" 
      android:orientation="vertical">  <---- Change this line 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Read More" 
       android:textSize="15dp" 
       android:textStyle="bold" 
       android:layout_gravity="center" 
       android:clickable="true" 
       android:focusable="false" 
       android:gravity="center" 
       android:onClick="readMore" 
       android:padding="1dp" 
       android:textColor="@android:color/holo_green_light" /> 
     </LinearLayout> 
+0

Es funktionierte, aber ich möchte Kind Ansichten im horizontalen Layout und dynamische TextView oberhalb der bereits vorhandenen TextView! –

+1

Ich denke, Sie sollten versuchen, die Anzahl der linearen und relativen Layouts zu reduzieren, es wird für die Leistung und sauberen Code gut sein. – RediOne1

+0

Ich möchte so etwas tun: 'für (int i = 0; ich

Verwandte Themen