gpt4 book ai didi

java - 表格不显示

转载 作者:行者123 更新时间:2023-12-01 04:40:52 25 4
gpt4 key购买 nike

我已经通过 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/

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