简体   繁体   中英

Android create a TextView

I'm new in Android and have a question.

How could I create a TextView in my MainActivity. How could I do this?

Thanks in advance!

Create a TextView in Java like this:

TextView tv = new TextView(context);
tv.setText("This is a text");
parentView.addView(tv);

Create a TextView in XML like this:

<TextView
    android:id="@+id/selected_iconpack_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="No Icon Pack Selected"/>

Try this.

In your layout file add a TextView object like this:

<TextView
    android:id="@+id/myTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!" />

in your Java file do this:

Declare a variable of type TextView and link it to the TextView in your layout

TextView myTextView = (TextView) findViewById(R.id.myTextView);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM