如何动态添加TableRows?

时间:2016-11-13 06:34:03

标签: android android-studio tablerow

我尝试创建一个新的表行并将其添加到现有的TableLayout中。我已经浏览了互联网,似乎我已经尝试了所有的东西,但我不能得到新的一行。如果我在布局中添加一个带有id的TableRow并将我的自定义ImageView添加到其中而不将新的TableRow添加到ImageView显示的布局,但我需要动态添加新行。在我的片段中:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                       Bundle savedInstanceState) {
  View rootView = inflater.inflate(R.layout.fragment_default_view, 
  container, false);

  final ViewGroup tableLayout = (ViewGroup) rootView.findViewById
     (R.id.table_bracket);

  TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(
          TableRow.LayoutParams.MATCH_PARENT,
          TableRow.LayoutParams.MATCH_PARENT
  );

  TableRow tableRow = new TableRow(context);
  tableRow.setLayoutParams(layoutParams);

  SeparatorImageView separator = new SeparatorImageView(getActivity());
  separator.setLayoutParams(layoutParams);

  tableRow.addView(separator);
  tableLayout.addView(tableRow);
  return rootView;
}

这是我尝试添加的布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.mbusby.bracketsandbox.DefaultViewFragment">

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/table_bracket"
  android:layout_width="match_parent"
  android:layout_height="fill_parent"
  android:stretchColumns="*">

</TableLayout>

1 个答案:

答案 0 :(得分:0)

在tableLayout.addView(tableRow)之后添加此行;线

getWindow().getDecorView().findViewById(android.R.id.content).invalidate();

刷新tableview新添加的行将可见

相关问题