【发布时间】:2015-03-16 14:03:22
【问题描述】:
我一直在尝试将 textview 动态添加到我的片段中,但这会导致应用程序崩溃,下面的代码在非片段活动中工作,我只是无法让它在片段中工作。目前我的代码只在 textview 上使用,但它最终会从数据库中返回很多,因此我需要动态创建它。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View theView = inflater.inflate(R.layout.fragment_list_batches, container, false);
context=getActivity();
//Dynamically create Elements
LinearLayout.LayoutParams SVView = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout SV = (LinearLayout)getActivity().findViewById(R.id.listBatchesRelative);
TextView batchName = new TextView(context);
int i = 1;
batchName.setId(Integer.valueOf(i));
batchName.setText("Dynamic Input view");
batchName.setLayoutParams(SVView);
SV.addView(batchName);
return theView
【问题讨论】:
标签: java android dynamic fragment