ย่อหน้าที่ 1 และ 2 วาดที่ XML
ย่อหน้าที่ 3 วาดที่ code
ย่อหน้าแรกกำหนดให้ข้อความกระจายชิดขอบทางขวาด้วย XML (กำหนด padding="20sp"
)
ย่อหน้าที่สองไม่ได้จัดการกระจาย ข้อความก็จะไม่ชิดขอบขวา
ย่อหน้าที่สามกำหนดให้ข้อความกระจายชิดขอบทางขวาด้วย Kotlin (กำหนด textView.setPadding(50, 50, 50, 50)
)
กำหนด padding
ใน XML กับใน Kotlin ค่าต่างกัน แต่ได้ผลใกล้เคียงกัน ‘-_-
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:id="@+id/ll_main_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/tvJustified" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:padding="20sp" android:justificationMode="inter_word" android:text="The text in this TextView is justified. This feature is introduced in Android version >= 8.0. The text in this TextView is justified. This feature is introduced in Android version >= 8.0.The text in this TextView is justified. This feature is introduced in Android version >= 8.0." /> <TextView android:id="@+id/tvNotJustified" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:padding="20sp" android:text="The text in this TextView is not justified. No justification. The text in this TextView is not justified. No justification. The text in this TextView is not justified. No justification. " /> </LinearLayout> </android.support.constraint.ConstraintLayout> |
บรรทัดที่ 21 : กำหนดให้ข้อความกระจายเต็มขอบทางขวา แต่คำสั่งนี้ใช้ได้กับ API Level 26 ขึ้นไป
MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.phaisarn.ktapplication import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.text.Layout.JUSTIFICATION_MODE_INTER_WORD import android.widget.TextView import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // creating TextView programmatically val textView = TextView(this) textView.setPadding(50, 50, 50, 50) textView.textSize = 20f textView.text = "This is a dynamic TextView generated programmatically in Kotlin, The text in this TextView is justified. This feature is introduced in Android version >= 8.0. The text in this TextView is justified. This feature is introduced in Android version >= 8.0.The text in this TextView is justified. This feature is introduced in Android version >= 8.0." textView.setJustificationMode(JUSTIFICATION_MODE_INTER_WORD) // add TextView to LinearLayout ll_main_layout.addView(textView) } } |
บรรทัดที่ 20 : กำหนดให้ข้อความกระจายเต็มขอบทางขวา แต่คำสั่งนี้ใช้ได้กับ API Level 26 ขึ้นไป
Link