2013-05-27 7 views
5

enter image description hereCome aggiungere a livello di codice 2 TextView in un layout lineare

Ciao a tutti,

sto ottenendo i seguenti campi "Nome" e "Skoda" da API. Ci sarà il numero x di oggetti come questo. Come da progetto, dovrei mostrarli come nell'immagine seguente.

Quindi, ho deciso di creare due textview a livello di codice in un layout lineare denominato "childLayout" come il seguente.

-- RelativeLayout 

    -- Linear Layout 
     -- TextView Textview -- 
    -- Linear Layout 

    -- Linear Layout 
     -- TextView Textview -- 
    -- Linear Layout  

    -- Linear Layout 
     -- TextView Textview -- 
    -- Linear Layout 

--RelativeLayout 

Ma non sto ottenendo l'output desiderato. Per favore aiutami a risolvere questo problema.

Ecco il codice:

TextView mType; 
TextView mValue;   
for (int i = 0; i < getDetailedDescAL.size(); i++) { 

    LinearLayout childLayout = new LinearLayout(
      DetailedCategories.this); 

    LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
      LayoutParams.WRAP_CONTENT, 
      LayoutParams.WRAP_CONTENT); 
    childLayout.setLayoutParams(linearParams); 

    mType = new TextView(DetailedCategories.this); 
    mValue = new TextView(DetailedCategories.this); 

    mType.setLayoutParams(new TableLayout.LayoutParams(
      LayoutParams.WRAP_CONTENT, 
      LayoutParams.WRAP_CONTENT, 1f)); 
    mValue.setLayoutParams(new TableLayout.LayoutParams(
      LayoutParams.WRAP_CONTENT, 
      LayoutParams.WRAP_CONTENT, 1f)); 

    mType.setTextSize(17); 
    mType.setPadding(5, 3, 0, 3); 
    mType.setTypeface(Typeface.DEFAULT_BOLD); 
    mType.setGravity(Gravity.LEFT | Gravity.CENTER); 

    mValue.setTextSize(16); 
    mValue.setPadding(5, 3, 0, 3); 
    mValue.setTypeface(null, Typeface.ITALIC); 
    mValue.setGravity(Gravity.LEFT | Gravity.CENTER); 

    mType.setText(getDetailedDescAL.get(i).getmPropertyType()); 
    mValue.setText(getDetailedDescAL.get(i).getmPropertyValue()); 

    childLayout.addView(mValue, 0); 
    childLayout.addView(mType, 0); 

    RelativeLayout.LayoutParams relativeParams = 
     new RelativeLayout.LayoutParams(
      LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
    relativeParams.addRule(RelativeLayout.BELOW); 
    Details.addView(childLayout, relativeParams); 

    // Details is the relative layout declared in XML 

} 

L'output è:

enter image description here

Sembra che i textviews hanno la priorità. Come risolvere questo.

+0

provare ad aggiungere le vostre opinioni in un LinearLayout con orientamento verticale anziché in una RelativeLayout. –

risposta

3

Sostituire il RelativeLayout per un LinearLayout e aggiungere tutti TextView's a quello. Non dimenticate di android:orientation="vertical" nel LinearLayout

+0

Grazie ha funzionato bene. Ho appena cambiato il layout principale come layout lineare. :-) –

0

modificare il layout relativa al layout lineare e sta risolto

+0

Grazie ha funzionato bene. Ho appena cambiato il layout principale come layout lineare. :-) con android: orientation = "vertical" –

0

JFI, perché non usate semplicemente ListView. In post, hai menzionato che il numero di elementi è sconosciuto. Quindi, può esserci un caso in cui si avrà un numero di elementi superiore a quello che si può avere sullo spazio visibile sullo schermo. Listview gestirà lo scrolling per te.

P.S .: Qui, invece di relativo o LinearLayout, utilizzare ScrollView

1
for(int j=0;j<30;j++) 
      { 

       LinearLayout childLayout = new LinearLayout(
          MainActivity.this); 

        LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
          LayoutParams.WRAP_CONTENT, 
          LayoutParams.WRAP_CONTENT); 
        childLayout.setLayoutParams(linearParams); 

        TextView mType = new TextView(MainActivity.this); 
        TextView mValue = new TextView(MainActivity.this); 

        mType.setLayoutParams(new TableLayout.LayoutParams(
          LayoutParams.WRAP_CONTENT, 
          LayoutParams.WRAP_CONTENT, 1f)); 
        mValue.setLayoutParams(new TableLayout.LayoutParams(
          LayoutParams.WRAP_CONTENT, 
          LayoutParams.WRAP_CONTENT, 1f)); 

        mType.setTextSize(17); 
        mType.setPadding(5, 3, 0, 3); 
        mType.setTypeface(Typeface.DEFAULT_BOLD); 
        mType.setGravity(Gravity.LEFT | Gravity.CENTER); 

        mValue.setTextSize(16); 
        mValue.setPadding(5, 3, 0, 3); 
        mValue.setTypeface(null, Typeface.ITALIC); 
        mValue.setGravity(Gravity.LEFT | Gravity.CENTER); 

        mType.setText("111"); 
        mValue.setText("111"); 

        childLayout.addView(mValue, 0); 
        childLayout.addView(mType, 0); 

        linear.addView(childLayout); 
      } 
Problemi correlati