我制作了自定义视图,我想添加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文件。有谁知道问题出在哪里?非常感谢。
答案 0 :(得分: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);
答案 1 :(得分: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);
}