自定义视图:嵌套的linearlayout未显示

时间:2011-06-16 19:04:36

标签: android

我创建了一个自定义视图。在其中,有一条线,一条文本视图,另一条线。在底线下方,我想要一个新的水平定向线性布局。当我运行它时,这个嵌套的linearlayout似乎根本没有显示出来。相反,我可以看到底线下方的测试按钮。我做错了什么?

    public class MyView extends LinearLayout   {

        public MyView(Context context, Question question) {
            super(context);



    //      this.setLayoutParams(params);
            this.setOrientation(VERTICAL);
            this.setBackgroundColor(Color.WHITE);
            LinearLayout.LayoutParams lineParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 2);
            View topLine = new View(context);
            lineParams.setMargins(0, 15, 0, 0);
            topLine.setBackgroundColor(Color.argb(255, 0, 159, 218));
            topLine.setLayoutParams(lineParams);

            this.addView(topLine);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

            //Challenge Question
            TextView questionText = new TextView(context);
            questionText.setTextColor(Color.BLACK);
            questionText.setTextSize(14);
            questionText.setLayoutParams(params);
            questionText.setGravity(Gravity.CENTER_HORIZONTAL);
            questionText.setText(question.getQuestion());

            this.addView(questionText);

            View bottomLine = new View(context);
            bottomLine.setBackgroundColor(Color.argb(255, 0, 159, 218));
            bottomLine.setLayoutParams(lineParams);


            this.addView(bottomLine);

            LinearLayout innerLayout = new LinearLayout(context);
            LinearLayout.LayoutParams innerLayoutParams = new LinearLayout.LayoutParams(300, LayoutParams.WRAP_CONTENT);
            innerLayout.setLayoutParams(innerLayoutParams);
            innerLayout.setBackgroundColor(Color.RED);
            innerLayout.setOrientation(HORIZONTAL);


            //TableLayout for the multiple choices  
            TableLayout tableLayout = new TableLayout(context);
            LayoutParams tableLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    //      tableLayoutParams.weight = .8f;
            tableLayout.setBackgroundColor(Color.RED);
            tableLayout.setLayoutParams(tableLayoutParams);


            innerLayout.addView(tableLayout);
            this.addView(innerLayout);

            Button button = new Button(context);
            button.setLayoutParams(params);
            button.setText("testing 123");
            this.addView(button);
}

请注意,我粘贴代码时没有添加到tablelayout的所有内容。我也许应该粘贴它。但是,当我这样做时它也没有用。但不管怎样,如果我将嵌套的linearlayout设置为300宽度并将背景颜色设置为红色,我至少应该看到它,不是吗?

1 个答案:

答案 0 :(得分:1)

考虑内部布局的高度应该是多少。现在它是wrap_content并且包含TableLayout(没有行),其高度也设置为wrap_content。在内部布局中似乎没有任何东西给它一个高度尺寸,所以这可能就是它没有被显示的原因。

尝试以下操作将使您的布局可见:

    LinearLayout.LayoutParams innerLayoutParams = new LinearLayout.LayoutParams(300, 300);

更有用的是,您可以尝试向TableLayout添加实际宽度/高度的内容。

另外,请考虑将您的布局用XML编写为better separate your application logic and the presentation

相关问题