<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" />
android:text="@string/hello_world"
<string name="hello_world">Hello world!</string>
<string name="hello_world"><i>Hello</i> world!</string>運行結果以下:
爲佈局文件activity_main.xml中的TextView添加id: html
<TextView android:id="@+id/myTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" />
package com.example.mytextview; import android.os.Bundle; import android.app.Activity; import android.text.Html; import android.text.Spanned; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView myTextView= (TextView) findViewById(R.id.myTextView); Spanned sp = Html.fromHtml("<h3><u>Hello world!</u></h3>"); myTextView.setText(sp); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
package com.example.mytextview; import android.os.Bundle; import android.app.Activity; import android.graphics.Color; // import android.graphics.Paint; // import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView myTextView= (TextView) findViewById(R.id.myTextView); myTextView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); myTextView.setTextSize(20); myTextView.setTextColor(Color.BLUE); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }