2011-03-11 3 views
0

Je veux montrer deux scrollView dans un LinearLayout ou RelativeLayout. Quelle propriété doit être définie pour afficher le premier Scrollview en haut de l'écran et le second scrollview juste en dessous du premier Scrollview?Montrer à ScrollView dans un LinearLayout

J'ai essayé mais en linearlayout il me montre seulement le premier scrollview et en relativelayout il ne me montre que le second scrollView. Oui, je veux faire tout cela dynamiquement sans utiliser de fichier XML.

Voici mon code

import android.app.Activity; 


import android.os.Bundle; 
import android.widget.*; 
import android.widget.TableLayout.LayoutParams; 

public class TableFinal extends Activity { 
    LinearLayout linearMain, linearScrollView, linearTextview; 
    RelativeLayout relativeMain; 
    ScrollView scrollview; 
    HorizontalScrollView Hscrollview; 
    TableLayout tablelayout; 
    TableRow tablerow; 
    TextView textview; 


    LinearLayout.LayoutParams linearparmas; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     linearparmas=new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT); 

     relativeMain = new RelativeLayout(this); 

     // First Table 
     linearScrollView = new LinearLayout(this); 
     scrollview = new ScrollView(this); 
     Hscrollview = new HorizontalScrollView(this); 
     tablelayout = new TableLayout(this); 

     // First Table's First Row 
     tablerow = new TableRow(this); 

     linearTextview = new LinearLayout(this); 
     textview = new TextView(this); 
     textview.setText("11"); 
     linearTextview.addView(textview); 
     tablerow.addView(linearTextview); 

     linearTextview = new LinearLayout(this); 
     textview = new TextView(this); 
     textview.setText("12"); 
     linearTextview.addView(textview); 
     tablerow.addView(linearTextview); 

     tablelayout.addView(tablerow); 
     Hscrollview.addView(tablelayout); 
     scrollview.addView(Hscrollview); 
     linearScrollView.addView(scrollview); 

     relativeMain.addView(linearScrollView); 
     // first table complete 

     // second tabler start 
     linearScrollView = new LinearLayout(this); 
     scrollview = new ScrollView(this); 
     Hscrollview = new HorizontalScrollView(this); 
     tablelayout = new TableLayout(this); 

     // second Table's First Row 
     tablerow = new TableRow(this); 

     linearTextview = new LinearLayout(this); 
     textview = new TextView(this); 
     textview.setText("21"); 
     linearTextview.addView(textview); 
     tablerow.addView(linearTextview); 

     linearTextview = new LinearLayout(this); 
     textview = new TextView(this); 
     textview.setText("22"); 
     linearTextview.addView(textview); 
     tablerow.addView(linearTextview); 

     tablelayout.addView(tablerow); 
     Hscrollview.addView(tablelayout); 
     scrollview.addView(Hscrollview); 
     linearScrollView.addView(scrollview); 

     relativeMain.addView(linearScrollView); 
     // second tabler complete 

     setContentView(relativeMain); 

    } 
} 

Merci.

+0

Pouvez-vous nous écrire votre code ici? Ce qui vous aidera plus. – Sandy

+0

Hé, ici j'ai mis mon code. – Nirav

Répondre

2

Vous pouvez utiliser LinearLayout ou RelativeLayout. Pour toutes les mises en page, vous devez afficher displayHeight.

Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
    int displayHeight = display.getHeight(); 

Ensuite, pour toutes les mises en page, configurez layoutParams pour remplir parent.

Maintenant pour scrollViews les différences commencent.

En LinearLayout pour les deux scrollViews fixent les mêmes layoutParams (LinearLayout.layoutParams)

scrollView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, displayHeight/2)); 

Et COMBINEZ à LinearLayout

Dans RelativeLayout mis layoutParams comme dans mon exemple (RelativeLayout.layoutParams)

  RelativeLayout.LayoutParams lp1 = new LayoutParams(LayoutParams.FILL_PARENT, displayHeight); 
      lp1.addRule(ALIGN_PARENT_TOP); 
      scrollView1.setLayoutParams(lp1); 
      RelativeLayout.LayoutParams lp2 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
      lp2.addRule(BELOW, scrollView1.getId()); 
      scrollView2.setLayoutParams(lp2); 

Et ajoutez les deux à la disposition.

Devrait fonctionner dans toute la résolution d'affichage si je n'ai pas foiré.

Questions connexes