このように、addViewメソッドを使用して動的にビューを挿入したいと思います。
TextView view = new TextView(this);
view.setText("foo");
ViewGroup parentView = (ViewGroup) findViewById(R.id.main_layout);
parentView.addView(view);
挿入したビューを下に揃えるにはどうすればよいですか?
このように、addViewメソッドを使用して動的にビューを挿入したいと思います。
TextView view = new TextView(this);
view.setText("foo");
ViewGroup parentView = (ViewGroup) findViewById(R.id.main_layout);
parentView.addView(view);
挿入したビューを下に揃えるにはどうすればよいですか?
これを試してみると、結果が得られます。
RelativeLayout parent = (RelativeLayout) findViewById(R.id.llayout);
TextView textView = new TextView(this);
textView.setText("foo");
RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, textView.getId());
textView.setLayoutParams(params);
parent.addView(textView);
RelativeLayout
次のように、の代わりにを使用してみてくださいViewGroup
。
RelativeLayout parent = (RelativeLayout) findViewById(R.id.layout_parent);
TextView tv = new TextView(this);
tv.setText("foo");
RelativeLayout.LayoutParams lp= new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, tv.getId());
tv.setLayoutParams(lp);
parent.addView(tv);
以下のコードは、テキストビューを下に揃えます
TextView view = new TextView(this);
view.setText("foo");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, view.getId());
view.setLayoutParams(params);
ViewGroup parentView = (ViewGroup) findViewById(R.id.viewgroup);
parentView.addView(view);