/HEAD>
|
テクパー2020 テクニカルヘルパー |
android アンドロイド入門 アプリ開発 |
|
◆ リストの配置 ・ main.xml に ListView を追加し、標準のリストを表示します ・ deflist.xml を作成し、リストの形状を指定します ・ イベントリスナーを組み込んで、リストの操作を取得します |
2. リソース定義を変更します
1)res/layout の 「 main.xml 」 の書き換え
・<TextView> は Hello World が表示されるのでコメントにします
・<ListView> を追加します
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- Hello Worldが表示されるためコメントにする -->
<!-- TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/ -->
<!-- ListViewのレイアウト定義を追加する -->
<ListView
android:id="@+id/def_list"
android:layout_width="fill_parent"
android:layout_height="320dip"
android:layout_margin="6pt"
android:background="#ffC0C0C0"
/>
</LinearLayout>
|
|
※ ListView の android:id="@+id/def_list"のID:def_listを DefineList.java のListView listView = (ListView) findViewById(R.id.def_list);で指定します |
android.R.attr のR.attrのコンスタント match_parent:親画面と同じ大きさ (fill_parentはmatch_parentに置換え) wrap_content:表示内容に合わせた大きさ // レイアウトの上、右、左、下の余白の指定 public static final int layout_margin // 背景の指定 public static final int background ("#rgb", "#argb", "#rrggbb", or "#aarrggbb") |
package com.proto.definelist;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class DefineList extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// アレーアダプターの生成(標準のレイアウト)
ArrayAdapter
|
android.widget.ArrayAdapter のArrayAdapterクラスの生成、指定 public ArrayAdapter (Context context, int textViewResourceId) // アレーアダプターに追加(オブジェクト) public void add (T object) |
android.R.layout のR.layoutのコンスタント public static final int simple_list_item_1 public static final int simple_list_item_2 public static final int simple_list_item_checked public static final int simple_list_item_multiple_choice public static final int simple_list_item_single_choice |
android.widget.ListView のListViewクラスの生成、指定 // リストビューに追加 (リスト項目) public void setAdapter (ListAdapter adapter) |
・新規のウィザードを選択から 「 Android XML File 」 を選択
・[次へ]ボタン をクリック
・新規 Android XML ファイル のフォルダーに 「 deflist.xml 」 を入力
・[完了]ボタン をクリック
2)生成された 「 deflist.xml 」 に記述します
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#0000FF"
android:textStyle="italic"
android:textSize="12pt"
/>
|
package com.proto.definelist;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class DefineList extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// アレーアダプターの生成(標準のレイアウト)
// ArrayAdapter
|
android.app.Activity のActivityクラスの指定 // XMLのビュー識別の指定(ビューID) public View findViewById (int id) (main.xmlの |
android.view.View.OnLongClickListener のnLongClickListenerの処理 // クリックリスナーの処理(オーバーライド) public abstract boolean onLongClick (View v) |
android.widget.TextView のTextViewクラスで指定 // 文字列の指定 (文字列) public final void setText (CharSequence text) // 文字色の指定 (色コード) public void setTextColor (int color) // 文字サイズの指定 (サイズ) public void setTextSize (float size) |
| Copyright (C) 2010 プログラミングのテクニックをあなたに!!(リトル・ヘルパー) All Rights Reserved. |