android textview ..,Android TextView

The Android TextView component is a View subclass which is capable of showing text.

Being a subclass of View the TextView component can be used in your Android app's GUI

inside a ViewGroup, or as the content view of an activity.

Creating a TextView

You can create a TextView instance either by declaring it inside a layout XML file or by

instantiating it programmatically. I will cover both ways of creating a TextView in the following

sections.

Creating a TextView in a Layout File

Creating a TextView inside an Android layout XML file is done by inserting a TextView

element into the layout file at the place where you want the TextView to be displayed. Here

is an example layout file declaring a TextView:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:paddingBottom="@dimen/activity_vertical_margin"

tools:context=".MainActivity">

android:id="@+id/textview"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

Notice the TextView XML element inside the RelativeLayout element. This TextView

element declares a TextView object. The other layout attributes are covered in my tutorial about

Android layout.

Once the layout file is used as the content view of an Activity

subclass you can obtain a reference to the TextView instance like this:

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

TextView textView = (TextView) findViewById(R.id.textview);

}

}

Creating a TextView Programmatically

You can also instantiate an Android TextView programmatically. Here is an Android TextView

instantiation example:

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

RelativeLayout relativeLayout =

(RelativeLayout) findViewById(R.id.rootlayout);

TextView textView = new TextView(this);

textView.setText("Hey, one more TextView");

relativeLayout.addView(textView);

}

}

This example creates a TextView instance, sets its text and adds it to the root layout of the activity.

Set The Text of The TextView

You can set the text to be displayed in the TextView either when declaring it in your layout file,

or by using its setText() method.

Here is an XML example showing how to set the text of a declared TextView in a layout file:

android:id="@+id/textview"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

The text is set via the android:text attribute. You can either set the text as attribute value

directly, or reference a text defined in the strings.xml as is the case in the example above.

Here is a TextView setText() example, setting the text programmatically:

textView.setText("Hey, one more TextView");

Displaying HTML in a TextView

The Android TextView component can also display HTML. Here is how you do that:

textView.setText(Html.fromHtml(

"

This text is bold and uses HTML

" +

"

This is italic .

"));

The HTML the TextView is able to display is limited, though. If you need to display more advanced

HTML, use the Android WebView.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值