Java 如何在片段中动态添加TableRow?
我是Android编程新手,目前我正在尝试在表布局中动态地插入一个文本视图片段。它在将课堂扩展到活动时起了作用。下面是我的代码片段。我错过什么了吗Java 如何在片段中动态添加TableRow?,java,android,eclipse,android-fragments,Java,Android,Eclipse,Android Fragments,我是Android编程新手,目前我正在尝试在表布局中动态地插入一个文本视图片段。它在将课堂扩展到活动时起了作用。下面是我的代码片段。我错过什么了吗 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_schedule, null); Tabl
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_schedule, null);
TableLayout ll = (TableLayout) getActivity().findViewById(R.id.tableLayout1);
TableRow tr= new TableRow(getActivity());
TextView tv1 = new TextView(getActivity());
tv1.setText("TEST NUMBER");
tv1.setTextColor(Color.WHITE);
tv1.setTextSize(20);
tv1.setPadding(5, 5, 5, 5);
tr.addView(tv1);
ll.addView(tr);
return view;
}
提前谢谢。- 代码中的内容::背景和文本颜色相同,因此无法查看文本
- 解决方案::更改文本颜色或背景
活动\u main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</FrameLayout>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableLayout>
fragmentOne.java
package com.example.test;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class fragmentOne extends Fragment{
public static fragmentOne newInstance(){
fragmentOne fragment = new fragmentOne();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_main, container, false);
TableLayout ll = (TableLayout) view.findViewById(R.id.table);
TableRow tr= new TableRow(getActivity());
TextView tv1 = new TextView(getActivity());
tv1.setText("TEST NUMBER");
tv1.setTextColor(Color.BLACK);
tv1.setTextSize(20);
tv1.setPadding(5, 5, 5, 5);
tr.addView(tv1);
ll.addView(tr);
return view;
}
}
快照:
您必须给出view.findViewById, 因为您要在视图中添加它
TableLayout ll = (TableLayout) view.findViewById(R.id.tableLayout1);
检查它肯定会对您有所帮助。这对我很有用:
@Override
public View onCreateView(LayoutInflater inf, ViewGroup container, Bundle state) {
View view = inf.inflate(R.layout.records_fragment, container, false);
TableLayout table = (TableLayout) view.findViewById(R.id.records);
// Do something with the table declare in fragment.
return view;
}
您是否面临上述代码的任何问题?
@Override
public View onCreateView(LayoutInflater inf, ViewGroup container, Bundle state) {
View view = inf.inflate(R.layout.records_fragment, container, false);
TableLayout table = (TableLayout) view.findViewById(R.id.records);
// Do something with the table declare in fragment.
return view;
}