1

我有一个以编程方式添加文本视图的示例我想做的是在 xml 中添加相同的文本视图,以便我可以将它定位在布局中的特定位置有没有办法将此代码转换为 xml?

        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);
4

1 回答 1

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);
于 2013-09-26T03:37:18.940 回答