如何将视图添加到片段中?
经:
LinearLayout ll = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.my_tab, null);
TextView tv = new TextView(this);
tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tv.setText("sample text");
ll.addView(tv);
setContentView(ll);
我可以添加文字。适用于某项活动。但是如何动态更改片段的布局。如果我尝试使用上面的版本,我得到的树标签不再可以移动了。
答案 0 :(得分:0)
LinearLayout ll = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.my_tab, null);
TextView tv = new TextView(this);
tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
tv.setText("sample text");
ll.addView(tv);
// now locate the fragment you want to add a view too by its tag name
SomeFragment someFragment = (SomeFragment) getFragmentManager().findFragmentByTag("someTag");
if (someFragment != null){
someFragment.getView().addView(ll);
}