1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GridLayoutでweightを利用してViewを画面幅いっぱいに表示する

Posted at

やりたいこと

android.support.v7.widget.GridLayoutを用いて、複数の要素を画面サイズに合わせて画面横幅いっぱいに表示したい。
要素ごとのwidthは可変である必要がある。

スクリーンショット 2018-06-17 12.43.19.png

コード

サポートライブラリを追加しておく。

build.gradle
dependencies {
    ...
    implementation 'com.android.support:gridlayout-v7:26.0.1'
    ...
}

レイアウトファイルではandroid.support.v7.widget.GridLayoutを追加。
要素のViewにlayout_columnWeightを指定してやる。

grid_sample.xml
<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <android.support.v7.widget.GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="4dp"

        app:columnCount="3"
        app:rowOrderPreserved="false"
        app:useDefaultMargins="true">

        <TextView
            android:layout_width="0dp"
            android:layout_height="100dp"
            app:layout_columnWeight="1"
            android:padding="5dp"
            android:gravity="center"
            android:background="#FF33B5E5"
            android:text="ABCDEFGHIJKL" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="100dp"
            app:layout_columnWeight="1"
            android:padding="5dp"
            android:gravity="center"
            android:background="#FF33B5E5"
            android:text="ABCDEFGHIJKL" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="100dp"
            app:layout_columnWeight="1"
            android:padding="5dp"
            android:gravity="center"
            android:background="#FF33B5E5"
            android:text="ABCDEFGHIJKL" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="100dp"
            app:layout_columnWeight="1"
            android:padding="5dp"
            android:gravity="center"
            android:background="#FF33B5E5"
            android:text="ABCDEFGHIJKL" />

    </android.support.v7.widget.GridLayout>
</android.support.constraint.ConstraintLayout>

ポイント

1. android.widget.GridLayoutではなくandroid.support.v7.widget.GridLayoutを利用する

前者ではlayout_columnWeightが利用できないので、後者を利用する。

2. layout_widthには0dpを指定する

layout_width:"wrap_content"でも一見上手く動作するのだが、テキストが一行で収まらない量になると要素がGridLayoutをはみ出してしまう。

スクリーンショット 2018-06-17 12.41.44.png

参考

GridLayoutでweightを使う
https://qiita.com/yysk/items/da65ae4c82c84d0c0449

[Android] GridLayout(GridViewではなく)すべての子を均等に伸ばす方法 android-layout android-gridview | CODE Q&A [日本語]
https://code.i-harness.com/ja/q/98d657

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?