Android-table-layout

提供:Dev Guides
移動先:案内検索

Androidテーブルレイアウト

'_ビューのグループを行と列に配置するAndroid TableLayout。 <TableRow>要素を使用して、テーブルに行を作成します。 各行にはゼロ個以上のセルがあります。各セルは1つのViewオブジェクトを保持できます。_

TableLayoutコンテナは、行、列、またはセルの境界線を表示しません。

テーブルレイアウト

TableLayoutの属性

以下は、TableLayoutに固有の重要な属性です-

Sr.No. Attribute & Description
1

android:id

これは、レイアウトを一意に識別するIDです。

2

android:collapseColumns

これは、折りたたむ列のゼロベースのインデックスを指定します。 列インデックスはコンマで区切る必要があります:1、2、5。

3

android:shrinkColumns

縮小する列のゼロベースのインデックス。 列インデックスはコンマで区切る必要があります:1、2、5。

4

android:stretchColumns

ストレッチする列のゼロベースのインデックス。 列インデックスはコンマで区切る必要があります:1、2、5。

この例では、簡単な手順で、テーブルレイアウトを使用して独自のAndroidアプリケーションを作成する方法を示します。 _Hello World Example_の章で作成したAndroidアプリケーションを変更するには、次の手順に従います-

Step Description
1 You will use Android Studio IDE to create an Android application and name it as demo under a package com.example.demo as explained in the Hello World Example chapter.
2 Modify the default content of res/layout/activity_main.xml file to include few widgets in table layout.
3 No need to modify string.xml, Android studio takes care of default constants
4 Run the application to launch Android emulator and verify the result of the changes done in the application.

以下は、変更されたメインアクティビティファイル src/com.example.demo/MainActivity.java の内容です。 このファイルには、基本的な各ライフサイクルメソッドを含めることができます。

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }

}

以下は res/layout/activity_main.xml ファイルの内容です-

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">

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

      <TextView
         android:text="Time"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_column="1"/>

      <TextClock
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/textClock"
         android:layout_column="2"/>

   </TableRow>

   <TableRow>

      <TextView
         android:text="First Name"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_column="1"/>

      <EditText
         android:width="200px"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
   </TableRow>

   <TableRow>

      <TextView
         android:text="Last Name"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_column="1"/>

      <EditText
         android:width="100px"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
   </TableRow>

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

      <RatingBar
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/ratingBar"
         android:layout_column="2"/>
   </TableRow>

   <TableRow
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"/>

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

      <Button
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Submit"
         android:id="@+id/button"
         android:layout_column="2"/>
   </TableRow>

</TableLayout>

以下は、2つの新しい定数を定義する res/values/strings.xml の内容です-

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">HelloWorld</string>
   <string name="action_settings">Settings</string>
</resources>

変更したばかりの* Hello World!アプリケーションを実行してみましょう。 環境設定中に *AVD を作成したと思います。 Android Studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 AndroidスタジオはAVDにアプリをインストールして起動し、セットアップとアプリケーションで問題がなければ、次のエミュレータウィンドウが表示されます-

Android TableLayout