动态创建表格布局(android)

发布于 2024-12-19 08:01:14 字数 1385 浏览 2 评论 0原文

我想创建内部表格布局并添加 1 行 2 列,下面的代码没有显示任何内容,为什么?

这是主要活动:

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>

更新:

好吧,最后我解决了这个问题,只需要将 TableRow.LayoutParams 与 TextViews 一起使用,而不是 LinearLayout.LayoutParams 或其他一些

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

Here's main activity:

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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱你不解释 2024-12-26 08:01:14

这是工作代码:

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

Here is the working 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);
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文