【问题标题】:Android TableLayout.addView() does not really workAndroid TableLayout.addView() 并没有真正起作用
【发布时间】:2015-01-27 04:26:00
【问题描述】:

我正在尝试使用 addView 方法为我的 TableLayout 填充一些视图。

这是我尝试做的:

private void createNewTable(){

  tableLayout = (TableLayout) getView().findViewById(R.id.tableLayout1);
  for(int i = 0; i < objectList.size(); i++){
            if(objectList.get(i).getType() == 0){
                tableLayout.addView(createLocationObjectInTable((LocationObject)objectList.get(i)), i);
            }
  }
}

还有 createObjectInList 方法:

private View createLocationObjectInTable(LocationObject locObject) {

    TableRow tr = new TableRow(getActivity());
    View v = LayoutInflater.from(getActivity()).inflate(R.layout.view_layout, tr, false);

    TextView textViewCityName = (TextView) v.findViewById(R.id.textView_City_Name);
    TextView textViewCityProvider = (TextView) v.findViewById(R.id.textView_City_Provider);
    textViewCityName.setText(locObject.getTitle());
    textViewCityProvider.setText(locObject.getSubTitle());

    return v;
}

但是视图没有显示在 tableLayout 中。 Logcat 没有给我任何错误信息,当我尝试在该行之后做一些事情时

tableLayout.addView(createLocationObjectInTable((LocationObject)objectList.get(i)), i);

什么都没发生。该应用程序在此行之后什么都不做。

希望有人能告诉我我的错误吗?

【问题讨论】:

  • 您不应该从 createLocationObjectInTable 方法返回 TableRow 视图 (tr) 而不是膨胀的 View v?
  • 即使我返回 tr,视图也不显示。我从 AsyncTask 中调用方法 createNewtable(),可能这会导致一些问题?

标签: java android fragment tablelayout


【解决方案1】:

您无需将视图添加到 TableRow,而只需将视图添加到 TableLayout。试试这个:

private View createLocationObjectInTable(LocationObject locObject) {
    ...
    textViewCityProvider.setText(locObject.getSubTitle());

    tr.addView(v);
    return tr;

}

【讨论】:

  • 我试过这个,但是在我返回 tr 之后,TableLayout 仍然没有显示任何视图。 tableLayout.addView 之后的代码也没有运行,app 只是停止了。
  • @Sascha_ 您是否将视图添加到 TableRow (tr.addView(v);)? “只是停止”是什么意思?活动在 LogCat 中启动时没有错误?
猜你喜欢
  • 1970-01-01
  • 2012-01-13
  • 2016-12-25
  • 1970-01-01
  • 2014-09-14
  • 1970-01-01
  • 1970-01-01
  • 2012-04-06
  • 1970-01-01
相关资源
最近更新 更多