レイアウトxmlを使用せずに、アプリを作成しようと考えています。
まず、最初に以下の様なレイアウトxmlを使ってアプリを作りました。
xml
1<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 3 android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 4 android:paddingRight="@dimen/activity_horizontal_margin" 5 android:paddingTop="@dimen/activity_vertical_margin" 6 android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" 7 android:weightSum="1" 8 android:orientation="vertical"> 9 10 <TableLayout 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:stretchColumns="0"> 14 15 <TableRow 16 android:layout_width="wrap_content" 17 android:layout_height="match_parent"> 18 19 <EditText 20 android:layout_width="fill_parent" 21 android:layout_height="fill_parent" 22 android:id="@+id/editText" /> 23 24 <Button 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:text="@string/display" 28 android:id="@+id/displayButton" 29 android:layout_column="1" /> 30 </TableRow> 31 32 </TableLayout> 33 34 <TextView 35 android:layout_width="match_parent" 36 android:layout_height="wrap_content" 37 android:id="@+id/textView" 38 android:layout_marginTop="60dp" 39 android:textColor="#ff0000" 40 android:text="ここに表示されます。" /> 41</LinearLayout>
テキスト入力域、ボタン、テキスト表示域があり、テキスト入力域に入力された文字列が、
ボタンを押すとテキスト表示域に赤文字で表示されるという簡単なアプリです。
次に、レイアウトxmlを使わずに同じアプリを実装してみました。
以下の様なプログラムを考えました。
Java
1 protected void onCreate(Bundle savedInstanceState) { 2 super.onCreate(savedInstanceState); 3 // レイアウトxmlを使わずに、プログラムで組み立てる。 4 // setContentView(R.layout.activity_main); 5 LinearLayout linearLayout = new LinearLayout(this); 6 // 縦方向に配置 7 linearLayout.setOrientation(LinearLayout.VERTICAL); 8 setContentView(linearLayout); 9 10 // テーブルレイアウト 11 TableLayout tableLayout = new TableLayout(this); 12 // 列の伸長 13 tableLayout.setColumnStretchable(0, true); 14 15 // テーブル要素 16 TableRow tableRow = new TableRow(this); 17 // テキスト入力域 18 EditText editText = new EditText(this); 19 // IDを設定 20 editText.setId(View.generateViewId()); 21 editTextID = editText.getId(); 22 23 24 // ボタン 25 Button button = new Button(this); 26 // ボタンのテキスト 27 button.setText("display"); 28 // ボタンのID 29 button.setId(View.generateViewId()); 30 buttonID = button.getId(); 31 // クリックリスナを登録 32 button.setOnClickListener(this); 33 34 // テキスト入力域とボタンをテーブル行に追加 35 tableRow.addView(editText); 36 tableRow.addView(button); 37 // テーブル行をテーブルレイアウトへ。 38 tableLayout.addView(tableRow); 39 40 // 表示域 41 TextView textView = new TextView(this); 42 // 幅と高さの設定 43 textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 44 // パディング 45 textView.setPadding(0, 60, 0, 0); 46 // 表示するテキストの色 47 textView.setTextColor(0xff0000); 48 // IDを設定 49 textView.setId(View.generateViewId()); 50 textViewID = textView.getId(); 51 // 初期値 52 textView.setText("ここに表示されます。"); 53 54 linearLayout.addView(textView); 55 // テーブルレイアウトをメインパネルへ 56 linearLayout.addView(tableLayout); 57 // 表示域をメインパネルへ 58 linearLayout.addView(textView); 59 }
これを動かしてみたところ、テキスト入力域、ボタンは表示されますが、テキスト表示域が表示されません。
テキストの表示にはボタンに登録したクリックリスナを使用していますが、リスナ自体は
ちゃんと動いているのを確認しました。
プログラムで動的にレイアウトを生成するにはどうすれば良いのでしょうか。
このプログラムに不備があればその箇所を教えて下さい。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/11/28 08:40
2015/11/28 08:51
2015/11/30 01:56
2015/11/30 03:22