java - 表格不显示

标签 java android xml tablelayout

我已经通过 TableLayout (id=myProducts_tableLayout) 以编程方式创建了一个表,但它没有显示在屏幕上。下面是 XML 代码。

#`<?xml version="1.0" encoding="utf-8"?>
<ScrollView 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:fillViewport="true">

  <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:background="#ffffff">

        <!--  Header  Starts-->
        <LinearLayout android:id="@+id/header"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@layout/header_gradient" 
                android:paddingTop="5dip"
                android:paddingBottom="5dip">
                <!-- Logo Start-->
                <ImageView android:src="@drawable/real_time_bidding"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="10dip"/>
                <!-- Logo Ends -->
        </LinearLayout>
        <!--  Header Ends -->  

        <!-- Footer Start -->
        <LinearLayout android:id="@+id/footer"
                android:layout_width="fill_parent"
                android:layout_height="90dip"
                android:background="@layout/footer"
                android:layout_alignParentBottom="true">
        </LinearLayout>
        <!-- Footer Ends -->

        <!-- All Products View -->
        <LinearLayout
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:padding="10dip"
              android:layout_below="@id/header">

              <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="#153E7E"
                android:textSize="25px"
                android:textStyle="bold"
                android:typeface="serif"
                android:gravity="center"
                android:text="My Products"/>

            <TableLayout 
                android:id="@+id/myProducts_tableLayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:stretchColumns="0"
                 >
            </TableLayout>

            <!-- Link to Enter New Product -->      
            <TextView android:id="@+id/link_to_allProducts" 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="213dip"
                android:layout_marginBottom="50dip"
                android:textColor="#153E7E"
                android:textSize="20px"
                android:textStyle="bold|italic"
                android:typeface="serif"
                android:gravity="center"
                android:text="Back to All Products"/>
        </LinearLayout>
        <!-- All Products View Ends -->

    </RelativeLayout>   
</ScrollView>
`

下面是.java代码

super.onCreate(savedInstanceState);
setContentView(R.layout.my_products);       

String[] sampleTxt = {"txt1", "txt2", "txt3"};
ScrollView sv = new ScrollView(this);
final TableLayout layout = TableLayout findViewById(R.id.myProducts_tableLayout);
layout.setLayoutParams(new TableLayout.LayoutParams(4, 5));

for(int i = 0; i < sources.length; i++)
{
    final TableRow tr = new TableRow(this);
    final TextView t = new TextView(this);
    final Button b = new Button(this);

    final LayoutParams textparams = new LayoutParams(400,40); // Width , height
    t.setLayoutParams(textparams);
    t.setTextSize(TypedValue.COMPLEX_UNIT_SP,10);
    t.setTextColor(Color.BLUE);

    final LayoutParams btnparams = new LayoutParams(20,20);
    btnparams.setMargins(10, 20, 15, 0);    // (left, top, right, bottom)
    b.setLayoutParams(btnparams);
    b.setBackgroundResource(R.drawable.delete);


    t.setText(sampleTxt[i]);

    b.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v) 
        {
            layout.removeView(tr);
        }
    });

    tr.addView(b);
    tr.addView(t);
    layout.addView(tr);
}

哪位大佬能指出一下错误吗?而且我也不知道为什么 Eclipse 要求将 TableLayout、TableRow、TextView、Button 设置为“最终”,否则它会给我一个错误。

谢谢

最佳答案

问题出在您没有复制的代码中。您可能有:

import android.view.ViewGroup.LayoutParams;

但你想要:

import android.widget.TableRow.LayoutParams;

我本以为第一个也可以工作,因为您只指定宽度和高度,但似乎事实并非如此。 TableRow.LayoutParams 是 ViewGroup.LayoutParams 的子类。当您调用 b.setLayoutParams() 或 t.setLayoutParams 时,您必须将其 父级 TableRow 的 LayoutParams 作为参数传递。

关于java - 表格不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16572734/

相关文章:

java - 如何使用 SPQR 和 Spring 注册 ResolverInterceptors

java - 当某些字节错误时,BitmapFactory.decodeStream(inputStream) 总是返回 null

android - 使用Android PhoneGap : Not able to create table in the database

android - 获取类似于 Android Studio 内置 Android Monitor 的命令行 `adb logcat` 输出

c# - 使用 LINQ 读取和有条件地写入复杂的 XML 文件

xml - Go XML 解码节点 N 的属性 X

java - 在 XFire 中解析 XML 会导致 CPU 高吗?

java - 无法让 tomcat 服务器运行 - startup.bat 什么都不做

java - 将netbeans导入到eclipse中

android - ARCore:模拟器和统一