【问题标题】:How do I add TableRows dynamically?如何动态添加 TableRows?
【发布时间】:2016-11-13 06:34:03
【问题描述】:

我正在尝试创建一个新的表格行并将其添加到现有的表格布局中。我已经搜索了互联网,似乎我已经尝试了所有方法,但我无法显示新行。如果我在布局中添加一个带有 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>

【问题讨论】:

    标签: android android-studio tablerow


    【解决方案1】:

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

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

    刷新tableview新添加的行会可见

    【讨论】:

    • 似乎不起作用。我正在使用一个片段,所以我写了同样的东西,但在它之前使用 getActivity()
    • 我直接在tableLayout.getView(tableRow)之后添加了这个:getActivity().getWindow().getDecorView().findViewById(android.R.id.content).invalidate();
    • 你为什么要使用视图组,你也可以在 tableLayout 中投射视图
    • 我读到这是一个很好的做法,以防万一它发生变化,我不必重构。我不认为这是一个问题,因为我也尝试过直接投射到 TableLayout 并且它仍然不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2014-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多