如何在android中以编程方式将表格布局放置在线性布局下方

时间:2013-09-14 06:16:28

标签: android android-layout

这是我在线性布局下面显示表格布局的代码。我将滚动视图设置为contentview.but只在屏幕上显示表格布局。布局中有任何问题。请帮助我谢谢。

 ScrollView scrollView = new ScrollView(this);
            LinearLayout ll = new LinearLayout(getApplicationContext());
    ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));

            Button btn1 = new Button(this);
            btn1.setText("Button1");
            ll.addView(btn1);
            btn1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            TableLayout resultLayout = new TableLayout(this);
            resultLayout.setStretchAllColumns(true);
            resultLayout.setShrinkAllColumns(true);

这里尝试将线性布局放置在表格布局上方,现在只显示行列式布局

 LinearLayout ll = new LinearLayout(getApplicationContext());
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));

        // Set the TextView for the Question
        TextView tv1 = new TextView(getApplicationContext());
        tv1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        tv1.setText("HI");
        ll.addView(tv1);
ScrollView scrollView = new ScrollView(this);
        TableLayout resultLayout = new TableLayout(this);
        resultLayout.setStretchAllColumns(true);
        resultLayout.setShrinkAllColumns(true);

//这里有表行代码

ll.addView(scrollView);
setContentView(ll);

现在根据您的答案将线性布局设置为内容视图滚动视图仅包含表格布局。但是为什么它不显示在屏幕上

2 个答案:

答案 0 :(得分:1)

android中的

ScrollView应该只包含一个孩子。因此,我建议您在LinearLayout中添加ScrollView,然后在此LinearLayout

中添加其他组件

答案 1 :(得分:0)

检查一下:\

LinearLayout ll = new LinearLayout(getApplicationContext()); 
ll.setOrientation(LinearLayout.VERTICAL); 
ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
LayoutParams.WRAP_CONTENT)); 

// Set the TextView for the Question 
TextView tv1 = new TextView(getApplicationContext()); 
tv1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
LayoutParams.WRAP_CONTENT)); 
tv1.setText("HI"); 
ll.addView(tv1); 
ScrollView scrollView = new ScrollView(this); 
scrollView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
LayoutParams.WRAP_CONTENT)); 
TableLayout resultLayout = new TableLayout(this); 
resultLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
LayoutParams.WRAP_CONTENT)); 
scrollView.addView(resultLayout);   
resultLayout.setStretchAllColumns(true); 
resultLayout.setShrinkAllColumns(true); 
ll.addView(scrollView);