setcontentview android中的代码和xml
我已经将我的setcontentview android中的代码和xml,android,user-interface,Android,User Interface,我已经将我的setContentView设置为一个视图(代码),但现在我需要一个xml来在其上添加一个按钮。是否可以将UI与代码和xml结合起来?如果可能的话,我应该怎么做并添加代码 代码如下: public class Diagram extends Activity { /** Called when the activity is first created. */ grafik mgrafik; @Override public void onCreate(Bundle
setContentView
设置为一个视图(代码),但现在我需要一个xml来在其上添加一个按钮。是否可以将UI与代码和xml结合起来?如果可能的话,我应该怎么做并添加代码
代码如下:
public class Diagram extends Activity {
/** Called when the activity is first created. */
grafik mgrafik;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mgrafik = new grafik(this);
setContentView( mgrafik);
}
}
谢谢您的帮助。假设
grafik
是一种视图
您可以将它放入您的xml中:
Alhamdulillah,最后我可以通过编码使用relativelayout将按钮和grafik组合起来。这是消息来源
public class Diagram extends Activity {
/** Called when the activity is first created. */
grafik mgrafik;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout rel = new RelativeLayout (this);
rel.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mgrafik = new grafik(this);
RelativeLayout.LayoutParams grf = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
grf.addRule(RelativeLayout.CENTER_HORIZONTAL);
Button btn = new Button(this);
btn.setText("Kembali");
RelativeLayout.LayoutParams pbtn = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
pbtn.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rel.addView(mgrafik, grf);
rel.addView(btn, pbtn);
setContentView(rel);
}
}
谢谢您的帮助。我已经在absoluteLayout上试用过了,没有错误。但当我在android模拟器上运行程序时,它说活动意外停止,需要关闭。还有其他想法吗?可能是因为你没有添加第二个构造函数。stacktrace说什么?我应该如何放置第二个构造函数?什么是stacktrace?是[2011-08-16 08:05:43-CobaPass]安装CobaPass.apk。。。[2011-08-16 08:06:25-科巴萨斯]成功!在设备模拟器上启动活动coba.pass.CobaPassActivity-5554[2011-08-16 08:06:27-CobaPass]ActivityManager:Starting:Intent{act=android.Intent.action.MAIN cat=[android.Intent.category.LAUNCHER]cmp=coba.pass/.CobaPassActivity}堆栈跟踪准确地告诉您出了什么问题。在xml中使用视图时,始终需要提供以下构造函数:public View(Context-Context,AttributeSet-attrs)我应该将该构造函数放在grafik.java还是Diagram.java上?我应该在构造函数中写什么?很抱歉打扰你,我是新手。谢谢