You are on page 1of 3

TextView

Project : TextViewDemo

สําหรับโปรเจ็กนี้ เราจะแนะนําให้รู้จักกับ TextView โดยที่ โปรเจ็กนี้


จะแสดงข้อความว่า HelloAndroid

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >

<TextView
android:id = "@+id/textView"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_gravity = "center_horizontal"
android:text = "Hello Android"
android:textSize = "14pt" >
</TextView>

</LinearLayout>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >

Code XML ในการจัด Layout ของ LinearLayout


orientation : อะไรก็ตามที่ใส่เข้าไปใน linear จะเรียงต่อกันตามแนวดิ่ง
width : กว้าง เท่ากับขนาดของ parent ในที่นี้ คือ จอภาพ
height : สูง เท่ากับขนาดของ parent ในที่นี้ คือ จอภาพ
gravity : อะไรก็ตามที่ อยู่ใน นี้ จะมีการจัดให้อยู่ตรงกลางของ Layout นี้
<TextView
android:id = "@+id/textView"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_gravity = "center_horizontal"
android:text = "Hello Android"
android:textSize = "14pt" >
</TextView>

Code XML ในการจัด Layout ของ TextView


id : มีชื่อตัวแปรว่า textView
width : กว้าง เท่ากับขนาดของข้อมูลที่อยู่ข้างใน ซึ่งในที่นี้ คือ Hello Android
height : สูง เท่ากับขนาดความสูงของข้อมูลที่อยู่ข้างใน ซึ่งในที่นี้ คือ Hello Android
gravity : ตําแหน่งของ TextView นี้ ให้อยู่ตรงกลางในแนว นอน
text : เป็นการกําหนดข้อความ ในที่นี้คือ Hello Android
textSize : ขนาดตัวอักษร 14 pt

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class TextViewDemo extends Activity {



TextView textView;

/** Called when the activity is first created. */


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

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

}
}

TextView textView;

Code Java ในการประกาศตัวแปร TextView


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

Code Java ในการการเชื่อม ตัวแปร TextView กับ Layout ( XML )

drbomkung@gmail.com

You might also like