0

我想在一个 LinearLayout 或 RelativeLayout 中显示两个 scrollView。应该将哪个属性设置为在屏幕顶部显示第一个 Scrollview 并在第一个 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);

    }
}

谢谢你。

4

1 回答 1

2

您可以使用线性布局或相对布局。对于所有布局,您需要获取 displayHeight。

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

然后为所有布局设置 layoutParams 以填充父级。

现在滚动视图的差异开始了。

在两个滚动视图的 LinearLayout 中设置相同的 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);

并将两者都添加到布局中。

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

于 2011-03-11T08:34:01.750 回答