2012年3月19日月曜日

TableLayout

前回のシステム情報はログ出力で確認しましたので、デバッガがないと確認できませんでした。これを画面に表示させてみましょう。
これまで表示系のサンプルはLinearLayoutを使ってきましたが、表のようなものは「TableLayout」を使うと便利です。

実行結果
Radioの行の列同士がくっついてしまいましたが、これはマージンやパディングを指定していないので、表示領域いっぱいに書いてしまうとこうなります。この辺りの対策方法はまた別途。

黄色の行はクラス名を表示するということで、2つの列を結合して、目立つように色も変更しました。それ以外は各行は2列、右の列は折りたたんで全体を表示するようにしています。

では、まずレイアウトファイルをどう記述するか見てみましょう。

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="1"
            android:shrinkColumns="1"
            >
            <TableRow android:background="#ffff00">
                <TextView
                    android:layout_span="2"
                    android:text="Build Information"
                    android:layout_gravity="center"
                    android:textColor="#000000"
                    />
            </TableRow>
            <TableRow>
                <TextView android:text="Board" />
                <TextView android:id="@+id/board" />
            </TableRow>
            <TableRow>
                <TextView android:text="Boot Loader" />
                <TextView android:id="@+id/boot_loader" />
            </TableRow>
            <TableRow>
                <TextView android:text="Brand" />
                <TextView android:id="@+id/brand" />
            </TableRow>
            <TableRow>
                <TextView android:text="CPU_ABI" />
                <TextView android:id="@+id/cpu_abi" />
            </TableRow>
            <TableRow>
                <TextView android:text="CPU_ABI2" />
                <TextView android:id="@+id/cpu_abi2" />
            </TableRow>
            <TableRow>
                <TextView android:text="Device" />
                <TextView android:id="@+id/device" />
            </TableRow>
            <TableRow>
                <TextView android:text="Display" />
                <TextView android:id="@+id/display" />
            </TableRow>
            <TableRow>
                <TextView android:text="Fingerprint" />
                <TextView android:id="@+id/fingerprint" />
            </TableRow>
            <TableRow>
                <TextView android:text="Hardware" />
                <TextView android:id="@+id/hardware" />
            </TableRow>
            <TableRow>
                <TextView android:text="Host" />
                <TextView android:id="@+id/host" />
            </TableRow>
            <TableRow>
                <TextView android:text="ID" />
                <TextView android:id="@+id/id" />
            </TableRow>
            <TableRow>
                <TextView android:text="Manufacturer" />
                <TextView android:id="@+id/manufacturer" />
            </TableRow>
            <TableRow>
                <TextView android:text="Model" />
                <TextView android:id="@+id/model" />
            </TableRow>
            <TableRow>
                <TextView android:text="Product" />
                <TextView android:id="@+id/product" />
            </TableRow>
            <TableRow>
                <TextView android:text="Radio (deprecated)" />
                <TextView android:id="@+id/radio" />
            </TableRow>
            <TableRow>
                <TextView android:text="Serial" />
                <TextView android:id="@+id/serial" />
            </TableRow>
            <TableRow>
                <TextView android:text="Tags" />
                <TextView android:id="@+id/tags" />
            </TableRow>
            <TableRow>
                <TextView android:text="Time" />
                <TextView android:id="@+id/time" />
            </TableRow>
            <TableRow>
                <TextView android:text="Type" />
                <TextView android:id="@+id/type" />
            </TableRow>
            <TableRow>
                <TextView android:text="User" />
                <TextView android:id="@+id/user" />
            </TableRow>
            <TableRow>
                <TextView android:text="Radio Version" />
                <TextView android:id="@+id/radio_version" />
            </TableRow>

            <TableRow android:background="#ffff00">
                <TextView
                    android:layout_span="2"
                    android:text="Build.VERSION Information"
                    android:layout_gravity="center"
                    android:textColor="#000000"
                    />
            </TableRow>
            <TableRow>
                <TextView android:text="Code Name" />
                <TextView android:id="@+id/codename" />
            </TableRow>
            <TableRow>
                <TextView android:text="Incremental" />
                <TextView android:id="@+id/incremental" />
            </TableRow>
            <TableRow>
                <TextView android:text="Release" />
                <TextView android:id="@+id/release" />
            </TableRow>
            <TableRow>
                <TextView android:text="SDK (deprecated)" />
                <TextView android:id="@+id/sdk" />
            </TableRow>
            <TableRow>
                <TextView android:text="SDK" />
                <TextView android:id="@+id/sdk_int" />
            </TableRow>
        </TableLayout>
    </ScrollView>
</LinearLayout>
TableRowタグで囲んだところが「1行」分です。

android:stretchColumnsは、列の幅を広げる時に指定します。指定方法は列のインデックス(0始まり)です。android:shrinkColumnsは、列の横幅に収まりきらない場合に折り返して格納するように指定します。こちらも列のインデックスで指定します。サンプルでは、1行につき2列ですから、"1"を指定すると右の列が対象となります。

クラス名を表示している行は2列を結合しています。これはandroid:layout_spanを使います。TableRowタグではなくて、中身の部分、TextViewで指定します。TextViewのある列とそれ以降の列を結合します。"2"を指定していますので、TextViewとその次の列の2つ分を結合する、という意味になります。TableRowタグのandroid:backgroundで1行分の背景色を変えています。文字色や表示位置はTextViewで変更します。

フィールド名は左の列に直接書きましたので、あとは右の列にコードで値をセットするだけです。

Androidプロジェクトの設定
プロジェクト名:TableLayoutTest1a
ビルドターゲット:Android 4.0
アプリケーション名:TableLayoutTest1a
パッケージ名:jp.co.triware.samples.TableLayoutTest1a
アクティビティーの作成:TableLayoutTest1aActivity
最小SDKバージョン:1
TableLayoutTest1aActivity.java
package jp.co.triware.samples.TableLayoutTest1a;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class TableLayoutTest1aActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Build
        ((TextView)findViewById(R.id.board)).setText(Build.BOARD);
        String bootloader;
        try {
            bootloader = Build.BOOTLOADER; // Lv8
        } catch (NoSuchFieldError e) {
            bootloader = "NoSuchFieldError";
        }
        ((TextView)findViewById(R.id.boot_loader)).setText(bootloader);
        ((TextView)findViewById(R.id.brand)).setText(Build.BRAND);
        String cpu_abi;
        try {
            cpu_abi = Build.CPU_ABI; // Lv4
        } catch (NoSuchFieldError e) {
            cpu_abi = "NoSuchFieldError";
        }
        ((TextView)findViewById(R.id.cpu_abi)).setText(cpu_abi);
        String cpu_abi2;
        try {
            cpu_abi2 = Build.CPU_ABI2; // Lv8
        } catch (NoSuchFieldError e) {
            cpu_abi2 = "NoSuchFieldError";
        }
        ((TextView)findViewById(R.id.cpu_abi2)).setText(cpu_abi2);
        ((TextView)findViewById(R.id.device)).setText(Build.DEVICE);
        String display;
        try {
            display = Build.DISPLAY; // Lv3
        } catch (NoSuchFieldError e) {
            display = "NoSuchFieldError";
        }
        ((TextView)findViewById(R.id.display)).setText(display);
        ((TextView)findViewById(R.id.fingerprint)).setText(Build.FINGERPRINT);
        String hardware;
        try {
            hardware = Build.HARDWARE; // Lv8
        } catch (NoSuchFieldError e) {
            hardware = "NoSuchFieldError";
        }
        ((TextView)findViewById(R.id.hardware)).setText(hardware);
        ((TextView)findViewById(R.id.host)).setText(Build.HOST);
        ((TextView)findViewById(R.id.id)).setText(Build.ID);
        String manufacturer;
        try {
            manufacturer = Build.MANUFACTURER; // Lv4
        } catch (NoSuchFieldError e) {
            manufacturer = "NoSuchFieldError";
        }
        ((TextView)findViewById(R.id.manufacturer)).setText(manufacturer);
        ((TextView)findViewById(R.id.model)).setText(Build.MODEL);
        ((TextView)findViewById(R.id.product)).setText(Build.PRODUCT);
        String radio = null;
        try {
            radio = Build.RADIO; // Lv8 (-> Lv14: method)
        } catch (NoSuchFieldError e) {
            radio = "NoSuchFieldError";
        }
        ((TextView)findViewById(R.id.radio)).setText(radio);
        String serial;
        try {
            serial = Build.SERIAL; // Lv9
        } catch (NoSuchFieldError e) {
            serial = "NoSuchFieldError";
        }
        ((TextView)findViewById(R.id.serial)).setText(serial);
        ((TextView)findViewById(R.id.tags)).setText(Build.TAGS);
        ((TextView)findViewById(R.id.time)).setText("" + Build.TIME); // long to string
        ((TextView)findViewById(R.id.type)).setText(Build.TYPE);
        ((TextView)findViewById(R.id.user)).setText(Build.USER);
        String radioVersion;
        try {
            radioVersion = Build.getRadioVersion(); // Lv14
        } catch (NoSuchMethodError e) {
            radioVersion = "NoSuchMethodError";
        }
        ((TextView)findViewById(R.id.radio_version)).setText(radioVersion);

        // Build.VERSION
        String codename;
        try {
            codename = Build.VERSION.CODENAME; // Lv4
        } catch (NoSuchMethodError e) {
            codename = "NoSuchFieldError";
        }
        ((TextView)findViewById(R.id.codename)).setText(codename);
        ((TextView)findViewById(R.id.incremental)).setText(Build.VERSION.INCREMENTAL);
        ((TextView)findViewById(R.id.release)).setText(Build.VERSION.RELEASE);
        ((TextView)findViewById(R.id.sdk)).setText(Build.VERSION.SDK);
        String sdk_int;
        try {
            sdk_int = "" + Build.VERSION.SDK_INT; // Lv4; int to string
        } catch (NoSuchMethodError e) {
            sdk_int = "NoSuchFieldError";
        }
        ((TextView)findViewById(R.id.sdk_int)).setText(sdk_int);
    }
}

0 件のコメント:

コメントを投稿