[Android]TableLayout、TableRowを使用するサンプル

2021年9月1日

説明
android:collapseColumns
列を折りたたむ
android:stretchColumns
画面内に収まるように列幅を広げて表示

1.アプリケーション MainActivity.java

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.tablelayout;
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);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
package com.arkgame.tablelayout; 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); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
package com.arkgame.tablelayout;

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);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

2.activity_main.xml 
属性
android:id 識別子ID
android:text 表示テキスト
<TableRow> <Button xxx /> </TableRow>
<TableRow> <TextView xxx /><EditText xxx /> </TableRow>

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<Button
android:id="@+id/backbutton"
android:text="Back"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<TextView
android:text="ユーザID"
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>
<TextView
android:text="パスワード"
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>
</TableLayout>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TableRow> <Button android:id="@+id/backbutton" android:text="Back" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </TableRow> <TableRow> <TextView android:text="ユーザID" 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> <TextView android:text="パスワード" 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> </TableLayout>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
   <TableRow>
      <Button
         android:id="@+id/backbutton"
         android:text="Back"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
   </TableRow>
   <TableRow>
      <TextView
         android:text="ユーザID"
         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>
      <TextView
         android:text="パスワード"
         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>
</TableLayout>

3.res/values/strings.xml
属性:app_name、action_settingsを指定

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TableLayoutサンプル</string>
<string name="action_settings">Settings</string>
</resources>
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">TableLayoutサンプル</string> <string name="action_settings">Settings</string> </resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>

   <string name="app_name">TableLayoutサンプル</string>
   <string name="action_settings">Settings</string>

</resources>

 

Android

Posted by arkgame