2011-03-11 75 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); 

并添加到布局。

如果我没有搞砸,应该在所有显示分辨率下工作。