คำอธิบายดูที่ 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." /> <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> |
MainActivity.java
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 |
package com.phaisarns.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.LinearLayout; import android.widget.TextView; import static android.text.Layout.JUSTIFICATION_MODE_INTER_WORD; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // creating TextView programmatically TextView textView = new TextView(this); textView.setPadding(50, 50, 50, 50); textView.setTextSize( 20f); textView.setText("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 LinearLayout ll_main_layout = findViewById(R.id.ll_main_layout); ll_main_layout.addView(textView); } } |