gpt4 book ai didi

java - 如何在 Fragment 中动态添加 TableRow?

转载 作者:行者123 更新时间:2023-11-30 02:44:02 27 4
gpt4 key购买 nike

我是 Android 编程的新手,目前我正在尝试在 fragment 中动态地在表格布局中插入一个 TextView 。它在将类扩展到 Activity 时起作用。下面是我的代码 fragment 。我错过了什么吗?

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

提前谢谢你。

最佳答案

  • 您的代码中有什么::您的背景和文本颜色相同,因此您无法查看文本
  • 解决方案::更改文本颜色或背景

我测试如下


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

包 com.example.test;

导入android.os.Bundle;导入 android.support.v4.app.Fragment;导入 android.support.v4.app.FragmentActivity;导入 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

关于java - 如何在 Fragment 中动态添加 TableRow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25346231/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com