java 如何以编程方式将 TableRow 从 xml 添加到 TableLayout?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14138793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add TableRow from xml to TableLayout programmatically?
提问by Chris95X8
Im new to Android and I'm trying to add a TableRow, which I already made with xml, to a TableLayout programmatically. I'm getting Force Closes, most are NullPointerExcpetions.
我是 Android 新手,我正在尝试以编程方式将我已经用 xml 制作的 TableRow 添加到 TableLayout。我得到了强制关闭,大多数是 NullPointerExcpetions。
Here's my java class
这是我的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;
}
}
This is my layout with the TableLayout:
这是我使用 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>
And the TableRow:
和 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>
I tried lots of different ways to achieve it but still nothing.
我尝试了很多不同的方法来实现它,但仍然没有。
回答by TNR
Replace code:
替换代码:
TableRow tr = (TableRow) view.findViewById(R.id.tr_row);
tl.addView(tr); //not working, obviously im missing something
with
和
View tr = inflater.inflate(R.layout.table_row_layout, null,false);
tl.addView(tr); //not working, obviously im missing something