0
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 3 years have passed since last update.

TableLayoutでボタンを画面いっぱいに表示する

Last updated at Posted at 2020-04-14

#横幅を画面いっぱいに広げたい
↓このボタンを横幅いっぱいに広げたい。
table1.PNG

元となるコード

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Button" />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Button" />
    </TableRow>

    <TableRow
        android:layout_width="0dp"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/button4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Button" />

        <Button
            android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Button" />
    </TableRow>
</TableLayout>

##stretchColumns
android:stretchColumns="列番号"を設定する。

activity_main.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="0,1">

table6.PNG
綺麗に広がりました。

#横幅を画面内に収めたい
今度はボタンが画面幅からはみ出てしまった場合。

table2.PNG

##shrinkColumns
android:shrinkColumns="列番号"を設定する。

activity_main.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:shrinkColumns="0,1,2,3,4">

table3.PNG
収まりました。

#縦幅を画面いっぱいに広げたい

TableRowにweight="1"を設定します。

activity_main.xml

<TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

table4.PNG

#縦横を画面にいっぱいに広げたい
shrinkColumnsとweight="1"を設定します。

activity_main.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:shrinkColumns="0,1,3,4">

<TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

table5.PNG

0
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
0
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?