0

我做了我的自定义视图,我想添加 view.button,所以我做了这个解决方案:

Public void onCreate(Bundle savedInstanceState)
{
    Button up;
    up = new Button(getApplicationContext());
    up.setText("ahoj");
    up.setHeight(100);
    up.setWidth(100);
    up.setTop(200);
    up.setLeft(100);
    LinearLayout layout = new LinearLayout(getApplicationContext());
    super.onCreate(savedInstanceState);
    setContentView(layout);

    myview view = new myview(this);
    layout.addView(view);

layout.addView(up);

我只看到我的视图,但没有按钮。我的观点只画了一些PNG文件。有谁知道问题出在哪里?非常感谢。

4

2 回答 2

3

最可能的原因是您的自定义视图添加了布局参数MATCH_PARENT。它占用了整个布局,并且按钮不可见。尝试使用WRAP_CONTENT参数添加您的自定义视图:

MyView view = new myview(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LAYOUTParams.WRAP_CONTENT)
layout.addView(view, lp);
于 2012-09-25T10:35:31.917 回答
0

您的代码正确,但顺序错误。试试这个:

Public void onCreate(Bundle savedInstanceState)
{
    Button up;
    LinearLayout layout = new LinearLayout(getApplicationContext());
    up = new Button(getApplicationContext());
    up.setText("ahoj");
    up.setHeight(100);
    up.setWidth(100);
    up.setTop(200);
    up.setLeft(100);




    myview view = new myview(this);
    layout.addView(view);
    layout.addView(up);

    setContentView(layout);
    super.onCreate(savedInstanceState);
}
于 2012-09-25T10:36:55.617 回答