动态创建tablelayout(机器人) [英] dynamically creating tablelayout (android)

查看:101
本文介绍了动态创建tablelayout(机器人)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建内部tablelayout和2列添加有1行,以下code显示什么,为什么?

I want to create inner tablelayout and add there 1 row with 2 columns, following code shows nothing, why?

下面的主要活动:

public class TestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT);
    TextView tv1 = new TextView(this);
    TextView tv2=new TextView(this);
    tv1.setLayoutParams(params);
    tv2.setLayoutParams(params);
    tv1.setText("Hello1!");
    tv2.setText("Hello2!");
    TableLayout layoutINNER = new TableLayout(this);
    layoutINNER.setLayoutParams(params);
    TableRow tr = new TableRow(this);
    tr.setLayoutParams(params);
    tr.addView(tv1);
    tr.addView(tv2);
    layoutINNER.addView(tr);
    LinearLayout main = (LinearLayout)findViewById(R.id.android_main_layout);
    main.addView(layoutINNER);
}
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/android:main_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</LinearLayout>


更新:


UPDATE:

好了,我终于解决了这个问题,它只是需要使用TableRow.LayoutParams与TextViews,不LinearLayout.LayoutParams或一些其他

Well, finally i solved this problem, it was just needed to use TableRow.LayoutParams with TextViews, not LinearLayout.LayoutParams or some other

推荐答案

下面是工作code:

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT);
    TextView tv1 = new TextView(this);
    TextView tv2=new TextView(this);
    android.widget.TableRow.LayoutParams trparams = new TableRow.LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT, android.widget.TableRow.LayoutParams.WRAP_CONTENT);
    tv1.setLayoutParams(trparams);
    tv2.setLayoutParams(trparams);
    tv1.setText("Hello1!");
    tv2.setText("Hello2!");
    TableLayout layoutINNER = new TableLayout(this);
    layoutINNER.setLayoutParams(params);
    TableRow tr = new TableRow(this);

    tr.setLayoutParams(params);
    tr.addView(tv1);
    tr.addView(tv2);
    layoutINNER.addView(tr);
    LinearLayout main = (LinearLayout)findViewById(R.id.android_main_layout);
    main.addView(layoutINNER);
}
}

这篇关于动态创建tablelayout(机器人)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆