【问题标题】:How to add TableRow from xml to TableLayout programmatically?如何以编程方式将 TableRow 从 xml 添加到 TableLayout?
【发布时间】:2012-12-17 19:24:12
【问题描述】:

我是 Android 新手,我正在尝试以编程方式将我已经使用 xml 制作的 TableRow 添加到 TableLayout。我正在强制关闭,大多数是 NullPointerExcpetions。

这是我的 java 类

public class DayFragment extends Fragment {

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


    TableLayout tl = (TableLayout) view.findViewById(R.id.tl);
    TableRow tr = (TableRow) view.findViewById(R.id.tr_row);

            tl.addView(tr); //not working, obviously im missing something

    //xml parsing stuff

            return view;
 }
}

这是我的 TableLayout 布局:

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="5dp" >

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:orientation="vertical" >

    <TableLayout
        android:id="@+id/tl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <include
            android:id="@+id/header"
            layout="@layout/table_header" />

        <include android:id="@+id/h_divider"
            layout="@layout/horizontal_divider"/>




    </TableLayout>
</ScrollView>

</HorizontalScrollView>

还有 TableRow:

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tr_row"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
    android:id="@+id/tv_hour"
    android:layout_weight="1"
    android:gravity="center"
    android:padding="8dp"
    android:textSize="15sp"
    android:textStyle="bold" />

<View
    android:layout_weight="1"
    android:background="@drawable/vertical_cell_divider" />

<TextView
    android:id="@+id/tv_subject"
    android:layout_weight="1"
    android:gravity="left"
    android:padding="8dp"
    android:textSize="18sp" />

<View
    android:layout_weight="1"
    android:background="@drawable/vertical_cell_divider" />

<TextView
    android:id="@+id/tv_start"
    android:layout_weight="1"
    android:gravity="left"
    android:padding="8dp"
    android:textSize="18sp" />

<TextView
    android:id="@+id/tv_bar"
    android:layout_weight="1"
    android:gravity="left"
    android:textSize="18sp" />

<TextView
    android:id="@+id/tv_end"
    android:layout_weight="1"
    android:gravity="left"
    android:padding="8dp"
    android:textSize="18sp" />

</TableRow>

我尝试了很多不同的方法来实现它,但仍然没有。

【问题讨论】:

    标签: java android xml tablelayout tablerow


    【解决方案1】:

    替换代码:

    TableRow tr = (TableRow) view.findViewById(R.id.tr_row);
    tl.addView(tr); //not working, obviously im missing something
    

    View tr = inflater.inflate(R.layout.table_row_layout, null,false);
    tl.addView(tr); //not working, obviously im missing something
    

    【讨论】:

    • 是的!非常感谢您解决了我的问题。你能告诉我如何多次添加相同的视图吗?孩子已经有一个父母,所以我不能通过放 tl.addView(tr); 来添加它;
    • 在 for 循环中重复代码。但请记住,在 for 循环中重复之前,您已经创建了视图数组。然后为数组中的每个视图膨胀 table_row_layout 并将其添加到表格布局中。
    • 我就是这样做的,它适用于 (int i=0;i
    • @TNR 你能帮我吗?我有tablelayout,然后是我使用xml添加的tablerow及其工作,但是在tablerow里面我有另一个tablelayout,然后是xml的tablerow,我很难添加第二个tablerow xml 你能帮帮我吗
    猜你喜欢
    • 2011-11-08
    • 1970-01-01
    • 2012-12-29
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    相关资源
    最近更新 更多