如何在片段中动态添加TableRow? [英] How to dynamically add a TableRow in a Fragment?

查看:162
本文介绍了如何在片段中动态添加TableRow?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android编程中是新的,目前我正试图以片段动态地在表格布局中插入一个textview。它在将类扩展到Activity时工作。以下是我的代码段。我缺少什么吗?

  public View onCreateView(LayoutInflater inflater,ViewGroup容器,Bundle savedInstanceState){
查看视图= inflater.inflate(R.layout.fragment_schedule,null);
TableLayout ll =(TableLayout)getActivity()。findViewById(R.id.tableLayout1);
TableRow tr = new TableRow(getActivity());
TextView tv1 = new TextView(getActivity());
tv1.setText(TEST NUMBER);
tv1.setTextColor(Color.WHITE);
tv1.setTextSize(20);
tv1.setPadding(5,5,5,5);
tr.addView(tv1);
ll.addView(tr);
返回视图;
}

提前谢谢。

解决方案

你必须给出view.findViewById,
,因为你在视图中添加了。

  TableLayout ll =(TableLayout)view.findViewById(R.id.tableLayout1); 

检查这篇文章,它肯定会帮助你。


I am new in Android programming, currently I am trying to insert a textview inside the table layout dynamically in fragment. It worked while extending the class to Activity. Below is my code snippet. Am I missing something?

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_schedule, null);
    TableLayout ll = (TableLayout) getActivity().findViewById(R.id.tableLayout1);
    TableRow tr= new TableRow(getActivity());
    TextView tv1 = new TextView(getActivity());
    tv1.setText("TEST NUMBER");
    tv1.setTextColor(Color.WHITE);
    tv1.setTextSize(20);
    tv1.setPadding(5, 5, 5, 5);
    tr.addView(tv1);
    ll.addView(tr);
    return view;
}

Thank you in advance.

解决方案

you have to give view.findViewById, because you are adding that in the view.

TableLayout ll = (TableLayout) view.findViewById(R.id.tableLayout1);

Check this post. it will surely help you.

这篇关于如何在片段中动态添加TableRow?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆