[Android]ListViewの要素を 中央寄せさせる方法

このページには広告が含まれる場合があります。

 

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)

(詳しくは、下の動画で解説していますので、よろしければご覧ください)

こんな記事も読まれています

今すぐ無料お試しキットを受け取る

はじめての方へ


みんプロ式プログラミング講座体系・ラインナップ
みんプロ式プログラミング講座受講の流れ(割引特典利用方法)
サイトマップ(みんプロHPコンテンツ一覧)
運営会社概要

みんプロ作成アプリ





今すぐ無料お試しキットを受け取る
TOP