gpt4 book ai didi

android - 自定义 View : nested linearlayout not showing up

转载 作者:行者123 更新时间:2023-11-29 02:12:33 26 4
gpt4 key购买 nike

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

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

请注意,我粘贴的代码没有添加到表格布局中的所有内容。我可能也应该粘贴那个。但是当我这样做时它也没有用。但无论哪种方式,如果我将嵌套线性布局设置为 300 宽度并为其设置红色背景色,我至少应该看到它,不是吗?

最佳答案

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

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

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

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

还可以考虑将您的布局以 XML 格式写入 better separate your application logic and the presentation .

关于android - 自定义 View : nested linearlayout not showing up,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6377170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com