我是Android编程的新手,目前我正在尝试在片段中动态地在表格布局中插入textview。它在将类扩展到Activity时起作用。以下是我的代码段。我错过了什么吗?
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;
}
提前谢谢。
答案 0 :(得分:1)
我测试过如下
<强> activity_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>
<强> fragment_main.xml 强>
<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>
<强> MainActivity.java 强>
package com.example.test;
导入android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment newFragment = fragmentOne.newInstance();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, newFragment).commit();
}
}
<强> 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;
}
}
<强>快照强> ::
答案 1 :(得分:0)
你必须提供view.findViewById, 因为你要在视图中添加它。
TableLayout ll = (TableLayout) view.findViewById(R.id.tableLayout1);
检查this post.它肯定会对您有帮助。
答案 2 :(得分:0)
这对我有用:
@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;
}