TextView を動的にフォントを大きくしたり、位置を変えるなどはコードで記述することで可能です。レイアウトファイルと併用することもできますが全てコードで書く場合はどうするか、ConstraintLayoutは少々面倒でした。
2021.1.1
TextView
TextViewをJavaコードでprogrammaticallyに記述することのメリットは、ダイナミックに変更できる点です。レイアウトXMLファイルは静的なので、一度作成すると変更できません。
一方デメリットとしては、コード量が増大しがちでレイアウトファイルのように感覚的に作ることもできません。ただ、レイアウトファイルで作成した後で部分的にコードで変更することは可能です。
 
コード化のポイント
テキストやボタンといった部品を配置するためには setContentView を使いますが、今回はレイアウトXMLファイルである activity_main.xml を使わないやり方です。
 
setContentView:
その名の通り、スクリーン画面にViewを設定するメソッドです。
設定されるViewはLayout形式でまとめられたViewグループになります。
 
![[Android]  TextViewをConstraintLayoutでレイアウトファイルを使わずに記述する 1x1.trans - [Android]  TextViewをConstraintLayoutでレイアウトファイルを使わずに記述する](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
setContentViewのメソッドは引数の取り方でいくつかあります
- setContentView(int layoutResID)
- レイアウトXMLファイルのIDを指定
 
- setContentView(View view)
- 簡略形です。今回はこれを使います
 
- setContentView(View view, ViewGroup.LayoutParams params)
Ref: Android Developer
 
例えば、デフォルトのレイアウトファイルではこのようになっていますのでそれぞれコードに置き換えてみます。
activity_main.xml
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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">     <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Hello World!"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintLeft_toLeftOf="parent"         app:layout_constraintRight_toRightOf="parent"         app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> | 
ConstraintLayoutのインスタンスを生成して setContentView に設定。
| 1 2 3 4 5 6 7 8 9 10 11 | // ConstraintLayoutのインスタンス作成 ConstraintLayout layout = new ConstraintLayout(this); // android:layout_width="match_parent" // android:layout_height="match_parent" layout.setLayoutParams(new ConstraintLayout.LayoutParams(     ConstraintLayout.LayoutParams.MATCH_PARENT,     ConstraintLayout.LayoutParams.MATCH_PARENT)); // setContentViewに設定 setContentView(layout); | 
 
TextView のインスタンスを作成
| 1 | TextView textView = new TextView(this); | 
 
表示するテキストを設定して
| 1 2 3 | // 本来はリソースに設定するところです String str = "Hello World!"; textView.setText(str); | 
 
TextViewの位置のConstraintはConstraintSetを使います。それを使うためTextViewのIDを設定する必要があるのでIDを作らないといけません
| 1 2 3 4 5 | int viewId = View.generateViewId(); textView.setId(viewId); ... textView.getId(); | 
 
ConstraintSetはクローンを作って制約を設定してapplyします。
| 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 34 35 36 37 38 39 40 41 42 43 44 | ConstraintSet constraintSet = new ConstraintSet(); constraintSet.clone(layout); // android:layout_width="wrap_content" constraintSet.constrainHeight(textView.getId(),         ConstraintSet.WRAP_CONTENT); //android:layout_height="wrap_content" constraintSet.constrainWidth(textView.getId(),         ConstraintSet.WRAP_CONTENT); // app:layout_constraintBottom_toBottomOf="parent" constraintSet.connect(                 textView.getId(),                 ConstraintSet.BOTTOM,                 ConstraintSet.PARENT_ID,                 ConstraintSet.BOTTOM,                 0); // app:layout_constraintLeft_toLeftOf="parent" constraintSet.connect(                 textView.getId(),                 ConstraintSet.LEFT,                 ConstraintSet.PARENT_ID,                 ConstraintSet.LEFT,                 0); // app:layout_constraintRight_toRightOf="parent" constraintSet.connect(                 textView.getId(),                 ConstraintSet.RIGHT,                 ConstraintSet.PARENT_ID,                 ConstraintSet.RIGHT,                 0); // app:layout_constraintTop_toTopOf="parent"  constraintSet.connect(                 textView.getId(),                 ConstraintSet.TOP,                 ConstraintSet.PARENT_ID,                 ConstraintSet.TOP,                 0); constraintSet.applyTo(layout); | 
サンプルコード
プロジェクトを作成しますが、プロジェクトの作り方の説明は「簡単な Button アプリを作る」を見てください
 
デフォルトでファイルが出来上がります。
MainActivity.java
これに以下のように記述していきます。
 
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | //package your.package.name; import androidx.appcompat.app.AppCompatActivity; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.constraintlayout.widget.ConstraintSet; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity {     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         //setContentView(R.layout.activity_main);         // Instance of ConstraintLayout         ConstraintLayout layout = new ConstraintLayout(this);         // set layout Width and Height as maximum         layout.setLayoutParams(new ConstraintLayout.LayoutParams(                 ConstraintLayout.LayoutParams.MATCH_PARENT,                 ConstraintLayout.LayoutParams.MATCH_PARENT));         // TextView instance         TextView textView = new TextView(this);         // Generate TextView ID         int viewId = View.generateViewId();         textView.setId(viewId);         String str = "Hello World!";         textView.setText(str);         layout.addView(textView);         ConstraintSet constraintSet = new ConstraintSet();         constraintSet.clone(layout);         // android:layout_width="wrap_content"         constraintSet.constrainHeight(textView.getId(),                 ConstraintSet.WRAP_CONTENT);         //android:layout_height="wrap_content"         constraintSet.constrainWidth(textView.getId(),                 ConstraintSet.WRAP_CONTENT);         // app:layout_constraintBottom_toBottomOf="parent"         constraintSet.connect(                 textView.getId(),                 ConstraintSet.BOTTOM,                 ConstraintSet.PARENT_ID,                 ConstraintSet.BOTTOM,                 0);         // app:layout_constraintLeft_toLeftOf="parent"         constraintSet.connect(                 textView.getId(),                 ConstraintSet.LEFT,                 ConstraintSet.PARENT_ID,                 ConstraintSet.LEFT,                 0);         // app:layout_constraintRight_toRightOf="parent"         constraintSet.connect(                 textView.getId(),                 ConstraintSet.RIGHT,                 ConstraintSet.PARENT_ID,                 ConstraintSet.RIGHT,                 0);         // app:layout_constraintTop_toTopOf="parent"         constraintSet.connect(                 textView.getId(),                 ConstraintSet.TOP,                 ConstraintSet.PARENT_ID,                 ConstraintSet.TOP,                 0);         constraintSet.applyTo(layout);         // ConstraintLayout set on ContentView         setContentView(layout);     } } | 
 
こんな感じで出来上がりました。
 
![[Android]  TextViewをConstraintLayoutでレイアウトファイルを使わずに記述する 1x1.trans - [Android]  TextViewをConstraintLayoutでレイアウトファイルを使わずに記述する](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
サンプルコード(修正)
テキストフォントが小さいので少し手を加えて見た目をよくしましょう。
 
TextViewの設定に以下を追加します。
| 1 2 3 4 5 6 7 8 9 | import android.util.TypedValue; import android.graphics.Color; ... // テキストカラー textView.setTextColor(Color.rgb(0x0, 0x0, 0xaa)); // テキストサイズ textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 50); | 
 
多少マシになりましたでしょうか。
 
![[Android]  TextViewをConstraintLayoutでレイアウトファイルを使わずに記述する 1x1.trans - [Android]  TextViewをConstraintLayoutでレイアウトファイルを使わずに記述する](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
関連ページ:
- TextView
Reference:
Android Developer
