2016-05-13 13 views
1

Ich muss Textview mit Json dynamisch erstellen. Für jetzt habe ich erfolgreich Click Here erstellen, aber nicht in der richtigen Weise.Android - Textview dynamisch in Reihe weise setzen

Wie dieses Bild aber nicht in der Lage zu erstellen. Ich mag das getan, Click Here

Hier xml-Code ist -

<LinearLayout 
      android:layout_marginTop="5dp" 
      android:layout_marginBottom="5dp" 
      android:id="@+id/layout_top_search" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

     </LinearLayout> 

Hier ist Java-Code

try { 
       JSONObject object = new JSONObject(response); 
       boolean status = object.getBoolean("status"); 
       if (status) { 
        JSONArray array = object.getJSONArray("topsearches"); 
        for (int i = 0; i < array.length(); i++) { 
         final TextView textView; 

         JSONObject jsonObject = array.getJSONObject(i); 
         String keyword = jsonObject.getString("search-keyord"); 
         String cat_id = jsonObject.getString("category_id"); 
         textView = new TextView(getActivity()); 
         textView.setText(keyword); 
         textView.setId(i); 
         textView.setBackgroundResource(R.drawable.border_gray); 
         textView.setPadding(10, 10, 10, 10); 
         textView.setTextSize(14); 
         textView.setLayoutParams(new LayoutParams 
           (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
         getLayout_top_search().addView(textView); 

         textView.setOnClickListener(new View.OnClickListener() { 
          @Override 
          public void onClick(View v) { 
           String query = textView.getText().toString(); 

          } 
         }); 

        } 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

Bitte helfen.

+0

Verwenden Listenansicht und laden Sie die Json-Array lsitview. einfache Art und Weise –

+1

@Sanwal Singh Wie wollen Sie, dass Textview zu sein .. Wie im ersten Bild oder wie der zweite. –

+0

Ich muss wie Bild 1, wenn ich Benutzer listview dann wird es im Listenformat wie Bild 2 kommen –

Antwort

3

ist Alles, was Sie wollen, ist Flow Layout

<com.wefika.flowlayout.FlowLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="start|top"> 

    <View 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Lorem ipsum" /> 

</com.wefika.flowlayout.FlowLayout> 

Statt LinearLayout Sie Flow Layout verwenden können, die Sie Textview in einer Reihe hinzuzufügen hilft. Wenn die Zeile nicht genügend Platz hat, wird eine neue Zeile erweitert. Und dann wird das Layout so aussehen.

Für weitere Informationen: Bitte lesen this und this

enter image description here

Bitte Fühlen Sie sich frei zu fragen, me.if Sie Problem konfrontiert sind.

+0

Wenn Sie dies verwenden, erhalten Sie einen Fehler. TextView Breite kommt match_parent. Wie setze ich TextView Breite als wrap_content? –

+0

Dies ist ein Fehler - java.lang.ClassCastException: android.widget.LinearLayout $ LayoutParams können nicht in com.wefika.flowlayout.FlowLayout $ LayoutParams –

+0

@SanwalSingh umgewandelt werden Es ist nicht notwendig, Layoutparameter hinzuzufügen. Bitte löschen Sie Ihre Textansicht Layoutparameter. –

1
TextView tvContent = new TextView(getActivity()); 
    medicineName.add(tvContent); 
    LinearLayout.LayoutParams tvParam = new LinearLayout.LayoutParams(
      ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    tvContent.setLayoutParams(tvParam); 
    tvContent.setGravity(Gravity.LEFT); 
    tvContent.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE); 
    tvContent.setText(name); 
    tvContent.setTextSize(16); 
    layout.addView(tvContent); 

Verwendung dieses in OnPostExecute() -Methode

+0

funktioniert nicht .... –

+0

können Sie bitte einen Fehler – Vinay

+0

no Fehler kommt .. –

0
 LinearLayout layout = new LinearLayout(getActivity()); 
    LinearLayout.LayoutParams lLayoutlayoutParams = new LinearLayout.LayoutParams(
      ViewGroup.LayoutParams.MATCH_PARENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT); 
    lLayoutlayoutParams.setMargins(0, 8, 8, 8); 
    layout.setLayoutParams(lLayoutlayoutParams); 
    layout.setPadding((int) (fDimRatio * 8), (int) (fDimRatio * 8), 
      (int) (fDimRatio * 8), (int) (fDimRatio * 8)); 
    layout.setOrientation(LinearLayout.VERTICAL); 



TextView tvContent = new TextView(getActivity()); 
    medicineName.add(tvContent); 
    LinearLayout.LayoutParams tvParam = new LinearLayout.LayoutParams(
      ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    tvContent.setLayoutParams(tvParam); 
    tvContent.setGravity(Gravity.LEFT); 
    tvContent.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE); 
    tvContent.setText(name); 
    tvContent.setTextSize(16); 
    layout.addView(tvContent); 


linearLayout.addView(layout); 

Linearlayout ist id von Linearlayout in xml

linearLayout = (LinearLayout) view.findViewById(R.id.container); 

fDimRatio Wert

private float fDimRatio = 1.0f; // /xml 
+0

Was ist das - fDimRatio? –

0

Wenn Sie wie Bild wollen 1 dann android ändern: orientation = "horizontal"

+0

Bereits getan, aber nicht funktioniert –

Verwandte Themen