Android 如何在fragmant中创建具有动态行的tabalelayout
创建具有多个列和行的表布局,列是静态的,但行是动态的,并且还显示边框 请尝试以下代码: 在XML中Android 如何在fragmant中创建具有动态行的tabalelayout,android,android-layout,android-fragments,Android,Android Layout,Android Fragments,创建具有多个列和行的表布局,列是静态的,但行是动态的,并且还显示边框 请尝试以下代码: 在XML中 在片段中(Java文件) main_table=view.findviewbyd(R.id.main_table); TableRow tr_head=新的TableRow(getContext()); tr_head.setLayoutParams(新的LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL\u父级, LinearL
在片段中(Java文件)
main_table=view.findviewbyd(R.id.main_table);
TableRow tr_head=新的TableRow(getContext());
tr_head.setLayoutParams(新的LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL\u父级,
LinearLayout.LayoutParams.WRAP_内容);
TextView label_target=新的TextView(getContext());
label_target.setText(“目标”);
label_target.setTextColor(Color.BLACK);
label_target.setTextSize(18);
label_target.setPadding(5,5,5,5);
tr_head.addView(label_target);//在此处将列添加到表行
TextView标签_completed=新的TextView(getContext());
标签_completed.setText(“已完成”);
标签完成。SettexSize(18);
标签完成。setTextColor(颜色为黑色);
标签_完成。设置填充(5,5,5,5);
tr_head.addView(标签已完成);
TextView label_all=新的TextView(getContext());
标签_all.setText(“全部”);
标签全部设置尺寸(18);
标签_all.setTextColor(颜色为黑色);
标签所有设置填充(5,5,5,5);
tr_head.addView(标签全部);
main_table.addView(tr_head,new TableLayout.LayoutParams(
LinearLayout.LayoutParams.FILL\u父级,
LinearLayout.LayoutParams.WRAP_内容);
text查看标签\目标\数据、标签\完成\数据、标签\所有\数据;
对于(int i=0;i请尝试以下代码:
在XML中
在片段中(Java文件)
main_table=view.findviewbyd(R.id.main_table);
TableRow tr_head=新的TableRow(getContext());
tr_head.setLayoutParams(新的LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL\u父级,
LinearLayout.LayoutParams.WRAP_内容);
TextView label_target=新的TextView(getContext());
label_target.setText(“目标”);
label_target.setTextColor(Color.BLACK);
label_target.setTextSize(18);
label_target.setPadding(5,5,5,5);
tr_head.addView(label_target);//在此处将列添加到表行
TextView标签_completed=新的TextView(getContext());
标签_completed.setText(“已完成”);
标签完成。SettexSize(18);
标签完成。setTextColor(颜色为黑色);
标签_完成。设置填充(5,5,5,5);
tr_head.addView(标签已完成);
TextView label_all=新的TextView(getContext());
标签_all.setText(“全部”);
标签全部设置尺寸(18);
标签_all.setTextColor(颜色为黑色);
标签所有设置填充(5,5,5,5);
tr_head.addView(标签全部);
main_table.addView(tr_head,new TableLayout.LayoutParams(
LinearLayout.LayoutParams.FILL\u父级,
LinearLayout.LayoutParams.WRAP_内容);
text查看标签\目标\数据、标签\完成\数据、标签\所有\数据;
对于(int i=0;i使用以下示例实现您想要的:
1)主活动类
public class MainActivity extends AppCompatActivity {
private final String TAG = MainActivity.class.getSimpleName();
private TableLayout tl;
private Button b_delete;
private Button b_add_row;
private List<List<String>> data;
private List<String> sample;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tl = (TableLayout) findViewById(R.id.tl);
b_delete = (Button) findViewById(R.id.b_delete);
b_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
deleteTable();
}
});
b_add_row = (Button) findViewById(R.id.b_add_row);
b_add_row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
data.add(sample);
notifyDataSetChanged();
}
});
sample = new ArrayList<>();
sample.add("Amazon India LTD");
sample.add("80");
sample.add("120");
data = new ArrayList<>();
for (int i = 0; i < 3; i++) {
List<String> d = new ArrayList<>();
for (int j = 0; j < 3; j++) {
d.add(sample.get(0));
d.add(sample.get(1));
d.add(sample.get(2));
}
data.add(d);
}
notifyDataSetChanged();
}
private void clearTable() {
tl.removeAllViews();
addFirstRow("Target", "Completed", "All");
}
private void deleteTable() {
clearTable();
data.clear();
//notify data set changed is not needed
}
private void addRow(String text, String text1, String text2,
int drawable, int drawable1, int drawable2) {
TableRow row = new TableRow(MainActivity.this);
row.setLayoutParams(new TableLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
TextView tv = new TextView(MainActivity.this);
tv.setText(text);
tv.setTextColor(ContextCompat.getColor(MainActivity.this, android.R.color.black));
tv.setPadding(5, 5, 5, 5);
tv.setBackground(ContextCompat.getDrawable(MainActivity.this, drawable));
TableRow.LayoutParams params = new TableRow.LayoutParams(0,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.weight = 1.0f;
tv.setLayoutParams(params);
TextView tv1 = new TextView(MainActivity.this);
tv1.setText(text1);
tv1.setGravity(Gravity.CENTER);
tv1.setTextColor(ContextCompat.getColor(MainActivity.this, android.R.color.black));
tv1.setPadding(5, 5, 5, 5);
tv1.setBackground(ContextCompat.getDrawable(MainActivity.this, drawable1));
TableRow.LayoutParams params1 = new TableRow.LayoutParams(0,
ViewGroup.LayoutParams.WRAP_CONTENT);
params1.weight = 0.5f;
tv1.setLayoutParams(params1);
TextView tv2 = new TextView(MainActivity.this);
tv2.setText(text2);
tv2.setGravity(Gravity.CENTER);
tv2.setTextColor(ContextCompat.getColor(MainActivity.this, android.R.color.black));
tv2.setPadding(5, 5, 5, 5);
tv2.setBackground(ContextCompat.getDrawable(MainActivity.this, drawable2));
TableRow.LayoutParams params2 = new TableRow.LayoutParams(0,
ViewGroup.LayoutParams.WRAP_CONTENT);
params2.weight = 0.5f;
tv2.setLayoutParams(params2);
row.addView(tv);
row.addView(tv1);
row.addView(tv2);
tl.addView(row);
}
private void addFirstRow(String text, String text1, String text2) {
addRow(text, text1, text2,
R.drawable.cell_shape_top_left,
R.drawable.cell_shape,
R.drawable.cell_shape_top_right);
}
private void addLastRow(String text, String text1, String text2) {
addRow(text, text1, text2,
R.drawable.cell_shape_bottom_left,
R.drawable.cell_shape,
R.drawable.cell_shape_bottom_right);
}
private void notifyDataSetChanged(){
clearTable();
for (int i = 0; i < data.size(); i++) {
if (i == (data.size() - 1)) {
addLastRow(data.get(i).get(0),
data.get(i).get(1),
data.get(i).get(2));
} else {
addRow(data.get(i).get(0),
data.get(i).get(1),
data.get(i).get(2),
R.drawable.cell_shape,
R.drawable.cell_shape,
R.drawable.cell_shape);
}
}
}
}
public类MainActivity扩展了AppCompatActivity{
private final String TAG=MainActivity.class.getSimpleName();
私人餐桌;
私人按钮b_删除;
专用按钮b_添加_行;
私人名单数据;
私人名单样本;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tl=(TableLayout)findviewbyd(R.id.tl);
b_delete=(按钮)findviewbyd(R.id.b_delete);
b_delete.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
deleteTable();
}
});
b_add_row=(按钮)findviewbyd(R.id.b_add_row);
b_add_row.setOnClickListener(新视图.OnClickListener()){
@凌驾
公共void onClick(视图){
数据。添加(样本);
notifyDataSetChanged();
}
});
示例=新的ArrayList();
添加样本(“亚马逊印度有限公司”);
样本。添加(“80”);
样本。添加(“120”);
数据=新的ArrayList();
对于(int i=0;i<3;i++){
列表d=新的ArrayList();
对于(int j=0;j<3;j++){
d、 添加(sample.get(0));
d、 添加(sample.get(1));
d、 添加(sample.get(2));
}
数据.添加(d);
}
notifyDataSetChanged();
}
私有void clearTable(){
tl.删除所有视图();
添加第一行(“目标”、“完成”、“全部”);
}
私有void deleteTable(){
clearTable();
data.clear();
//不需要通知已更改的数据集
}
私有void addRow(字符串文本、字符串文本1、字符串文本2、,
整数可提取,整数可提取1,整数可提取2){
TableRow row=新的TableRow(MainActivity.this);
row.setLayoutParams(新的TableLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_父级,
ViewGroup.LayoutParams.MATCH_PARENT);
TextView tv=新的TextView(MainActivity.this);
tv.setText(文本);
tv.setTextColor(ContextCompat.getColor(MainActivity.this,android.R.color.black));
设置填充(5,5,5,5);
tv.setBackground(ContextCompat.getDrawable(MainActivity.this,drawable));
TableRow.LayoutParams params=新建TableRow.LayoutParams(0,
ViewGroup.LayoutParams.WRAP_内容);
参数重量=1.0f;
tv.setLayoutParams(参数);
TextView tv1=新的TextView(MainActivity.this);
tv1.setText(text1);
tv1.setGravity(重心);
tv1.setTextColor(ContextCompat.getColor(MainActivity.this,android.R.color.black));
tv1.设置填充(5,5,5,5);
tv1.setBackground(ContextCompat.getDrawable(MainActivity.this,drawable1));
TableRow.LayoutParams参数1=新建TableRow.LayoutParams(0,
main_table=view.findViewById(R.id.main_table);
TableRow tr_head = new TableRow(getContext());
tr_head.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
TextView label_target = new TextView(getContext());
label_target.setText("Target");
label_target.setTextColor(Color.BLACK);
label_target.setTextSize(18);
label_target.setPadding(5, 5, 5, 5);
tr_head.addView(label_target);// add the column to the table row here
TextView label_completed = new TextView(getContext());
label_completed.setText("Completed");
label_completed.setTextSize(18);
label_completed.setTextColor(Color.BLACK);
label_completed.setPadding(5, 5, 5, 5);
tr_head.addView(label_completed);
TextView label_all = new TextView(getContext());
label_all.setText("All");
label_all.setTextSize(18);
label_all.setTextColor(Color.BLACK);
label_all.setPadding(5, 5, 5, 5);
tr_head.addView(label_all);
main_table.addView(tr_head, new TableLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
TextView label_target_data,label_completed_data,label_all_data;
for (int i = 0; i <2; i++) {
TableRow row= new TableRow(getContext());
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
label_target_data = new TextView(getContext());
label_target_data.setText("Target"+i);
label_target_data.setTextSize(15);
label_target_data.setTextColor(Color.BLACK);
label_target_data.setPadding(5, 5, 5, 5);
row.addView(label_target_data);
label_completed_data = new TextView(getContext());
label_completed_data.setText("Completed"+i);
label_completed_data.setTextColor(Color.BLACK);
label_completed_data.setTextSize(15);
label_completed_data.setPadding(5, 5, 5, 5);
row.addView(label_completed_data);
label_all_data = new TextView(getContext());
label_all_data.setText("All"+i);
label_all_data.setTextColor(Color.BLACK);
label_all_data.setTextSize(15);
label_all_data.setPadding(5, 5, 5, 5);
row.addView(label_all_data);
main_table.addView(row, new TableLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
}
public class MainActivity extends AppCompatActivity {
private final String TAG = MainActivity.class.getSimpleName();
private TableLayout tl;
private Button b_delete;
private Button b_add_row;
private List<List<String>> data;
private List<String> sample;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tl = (TableLayout) findViewById(R.id.tl);
b_delete = (Button) findViewById(R.id.b_delete);
b_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
deleteTable();
}
});
b_add_row = (Button) findViewById(R.id.b_add_row);
b_add_row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
data.add(sample);
notifyDataSetChanged();
}
});
sample = new ArrayList<>();
sample.add("Amazon India LTD");
sample.add("80");
sample.add("120");
data = new ArrayList<>();
for (int i = 0; i < 3; i++) {
List<String> d = new ArrayList<>();
for (int j = 0; j < 3; j++) {
d.add(sample.get(0));
d.add(sample.get(1));
d.add(sample.get(2));
}
data.add(d);
}
notifyDataSetChanged();
}
private void clearTable() {
tl.removeAllViews();
addFirstRow("Target", "Completed", "All");
}
private void deleteTable() {
clearTable();
data.clear();
//notify data set changed is not needed
}
private void addRow(String text, String text1, String text2,
int drawable, int drawable1, int drawable2) {
TableRow row = new TableRow(MainActivity.this);
row.setLayoutParams(new TableLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
TextView tv = new TextView(MainActivity.this);
tv.setText(text);
tv.setTextColor(ContextCompat.getColor(MainActivity.this, android.R.color.black));
tv.setPadding(5, 5, 5, 5);
tv.setBackground(ContextCompat.getDrawable(MainActivity.this, drawable));
TableRow.LayoutParams params = new TableRow.LayoutParams(0,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.weight = 1.0f;
tv.setLayoutParams(params);
TextView tv1 = new TextView(MainActivity.this);
tv1.setText(text1);
tv1.setGravity(Gravity.CENTER);
tv1.setTextColor(ContextCompat.getColor(MainActivity.this, android.R.color.black));
tv1.setPadding(5, 5, 5, 5);
tv1.setBackground(ContextCompat.getDrawable(MainActivity.this, drawable1));
TableRow.LayoutParams params1 = new TableRow.LayoutParams(0,
ViewGroup.LayoutParams.WRAP_CONTENT);
params1.weight = 0.5f;
tv1.setLayoutParams(params1);
TextView tv2 = new TextView(MainActivity.this);
tv2.setText(text2);
tv2.setGravity(Gravity.CENTER);
tv2.setTextColor(ContextCompat.getColor(MainActivity.this, android.R.color.black));
tv2.setPadding(5, 5, 5, 5);
tv2.setBackground(ContextCompat.getDrawable(MainActivity.this, drawable2));
TableRow.LayoutParams params2 = new TableRow.LayoutParams(0,
ViewGroup.LayoutParams.WRAP_CONTENT);
params2.weight = 0.5f;
tv2.setLayoutParams(params2);
row.addView(tv);
row.addView(tv1);
row.addView(tv2);
tl.addView(row);
}
private void addFirstRow(String text, String text1, String text2) {
addRow(text, text1, text2,
R.drawable.cell_shape_top_left,
R.drawable.cell_shape,
R.drawable.cell_shape_top_right);
}
private void addLastRow(String text, String text1, String text2) {
addRow(text, text1, text2,
R.drawable.cell_shape_bottom_left,
R.drawable.cell_shape,
R.drawable.cell_shape_bottom_right);
}
private void notifyDataSetChanged(){
clearTable();
for (int i = 0; i < data.size(); i++) {
if (i == (data.size() - 1)) {
addLastRow(data.get(i).get(0),
data.get(i).get(1),
data.get(i).get(2));
} else {
addRow(data.get(i).get(0),
data.get(i).get(1),
data.get(i).get(2),
R.drawable.cell_shape,
R.drawable.cell_shape,
R.drawable.cell_shape);
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:padding="10dp"
android:id="@+id/tl">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="Target"
android:layout_width="0dp"
android:padding="5dp"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:background="@drawable/cell_shape_top_left"
android:layout_weight="1.0"
android:layout_column="1" />
<TextView
android:text="Completed"
android:layout_width="0dp"
android:gravity="center"
android:padding="5dp"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:background="@drawable/cell_shape"
android:layout_weight="0.5"
android:layout_column="2" />
<TextView
android:text="All"
android:layout_width="0dp"
android:gravity="center"
android:padding="5dp"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:background="@drawable/cell_shape_top_right"
android:layout_weight="0.5"
android:layout_column="3" />
</TableRow>
</TableLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Delete"
android:id="@+id/b_delete">
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Row"
android:id="@+id/b_add_row">
</Button>
</LinearLayout>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape= "rectangle" >
<solid android:color="@android:color/white"/>
<stroke android:width="1dp"
android:color="@android:color/black"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape= "rectangle" >
<solid android:color="@android:color/white"/>
<corners
android:topLeftRadius="10dp">
</corners>
<stroke android:width="1dp"
android:color="@android:color/black"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape= "rectangle" >
<solid android:color="@android:color/white"/>
<corners
android:topRightRadius="10dp">
</corners>
<stroke android:width="1dp"
android:color="@android:color/black"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape= "rectangle" >
<solid android:color="@android:color/white"/>
<corners
android:bottomLeftRadius="10dp">
</corners>
<stroke android:width="1dp"
android:color="@android:color/black"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape= "rectangle" >
<solid android:color="@android:color/white"/>
<corners
android:bottomRightRadius="10dp">
</corners>
<stroke android:width="1dp"
android:color="@android:color/black"/>
</shape>