我有一个以编程方式添加textview的示例我想要做的是在xml中添加相同的textview以便我可以将它放在布局中的特定位置是否有办法将此代码转换为xml?< / p>
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
答案 0 :(得分:0)
应该是以下
//activity_mylayout.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40sp" />
//MyActivity.java
setContentView(R.layout.activity_mylayout); // Sets the xml layout as the view
TextView myTextView = (TextView)findViewById(R.id.my_textview); // Gets a reference to a component using the id
myTextView.setText(message);