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 に答える