LayoutInflarer
LayoutInflaterをちょっと使ってみた。
それぞれ追記分だけ記述。
Layoutの親ビューはLinearLayout(vertid
main.xml
プロジェクト作成で自動生成されるmainのレイアウトに、
ボタンとスクロールビューを足した。
中程にあるLinearLayout(id=inflateScreen)にinflateしたViewを表示させる。
Buttonのメソッド名はactivateInflaterとした。
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Layout Inflate!!!" android:textSize="20dp" android:onClick="activateInflater"/> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/inflateScreen"/> </ScrollView>
inflater_layout.xml
インフレーター用レイアウトファイル。
とりあえず TextView他ウィジェットを適当にレイアウトしてみた。
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Inflate TextView" android:textSize="36dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <DigitalClock android:layout_width="wrap_content" android:layout_height="wrap_content" /> <AnalogClock android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
>|Java|
//プライベート領域で変数宣言
private LinearLayout inflateScreen;
//onCreate内でfindViewById()して、実体を取得
inflateScreen =
(LinearLayout)findViewById(R.id.inflateScreen);
public void activateInflater(View view){
LayoutInflater layoutInflater = getLayoutInflater();
View inflateView =
layoutInflater.inflate(R.layout.inflate_layout,null);
inflateScreen.addView(inflateView);
}
|