2011-03-11 46 views
0

我想在一個LinearLayout或RelativeLayout中顯示兩個scrollView。 應將哪個屬性設置爲在屏幕頂部顯示第一個滾動視圖,並在第一個滾動視圖下方顯示第二個滾動視圖?在一個LinearLayout中顯示爲ScrollView

我已經嘗試,但在linearlayout它顯示我只有第一個scrollview和在relativelayout它顯示我只有第二個scrollView。

是的,我想動態地做所有這些,而不使用XML文件。

這裏是我的代碼

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); 

    } 
} 

謝謝。

+0

你可以在這裏發佈你的代碼嗎?這會幫助你更多。 – Sandy 2011-03-11 06:30:36

+0

嘿,在這裏我已經把我的代碼。 – Nirav 2011-03-11 10:47:55

回答

2

您可以使用LinearLayout或RelativeLayout。 對於所有需要獲取displayHeight的佈局。

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

然後爲所有佈局設置layoutParams以填充父項。

現在爲scrollViews差異開始。

在LinearLayout中兩個scrollViews設置相同的LayoutParams(LinearLayout.layoutParams)

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

而且兩者增加的LinearLayout

在RelativeLayout的在我的例子設置像的LayoutParams(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); 

並添加到佈局。

如果我沒有搞砸,應該在所有顯示分辨率下工作。