如何在片段中动态添加TableRow?

时间:2014-08-17 04:06:24

标签: java android eclipse android-fragments

我是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;
}

提前谢谢。

3 个答案:

答案 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;
    }

}

<强>快照 ::

enter image description here

答案 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;
}