このページには広告が含まれる場合があります。
AndroidでListViewを使うと、その中にある要素はデフォルトでは左端に寄っていますが、これを中央寄せにしたい場合の対処法です。
もくじ
1)リスト1行分の新しいレイアウトファイル作成
2)1のレイアウトの中身を作成

(コード例)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp">
<TextView
android:id="@+id/textListItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="8dp"
android:text="TextView"
android:textSize="18sp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
3)ArrayAdapterの初期化コードの変更
adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, wordList)
を
adapter = ArrayAdapter<String>(this, R.layout.list_row, R.id.textListItem, wordList)
に
(詳しくは、下の動画で解説していますので、よろしければご覧ください)