我试图在代码和java中设计。 这是活动的xml:如何部署Java和.XML?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:id="@+id/hello">
</RelativeLayout>
这是Java代码,我想要。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout=(RelativeLayout)findViewById(R.id.hello);
TextView txt=new TextView(this);
txt.setText("Hello World Problem");
layout.addView(txt);
setContentView(R.layout.activity_main);
}
在相对布局中,我想在java代码中添加textview。但上面的java代码不起作用。错误:应用程序已不幸停止。我如何编码?
哇。凉。愚蠢的我。天才你。感谢问:-)它的工作。 –