我拼命尝试以编程方式设置表中单元格的 TextView 属性,但无法使其工作!每当我设置布局属性时,该字段将不会出现(但不会给出任何错误或异常)。我将其归结为这个简单的例子:
package mmo.application.listpro;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class Test extends Activity
{
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout table = new TableLayout(this);
TableRow row = new TableRow(this);
for (String label: new String[] {"field1", "field2", "field3"}) {
TextView tv = new TextView(this);
tv.setText(label);
// LinearLayout.LayoutParams lp =
// new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
// LinearLayout.LayoutParams.WRAP_CONTENT);
// lp.setMargins(2, 2, 2, 2); // left/top/right/bottom
// tv.setLayoutParams(lp);
row.addView(tv);
}
table.addView(row);
setContentView(table);
}
}
这将显示三个字段,但是当您取消注释五个注释行时,什么也不会出现。为什么会这样?为什么设置布局参数会导致我的 TextView 不显示?我被困住了!我缺少什么?
Michael
PS.:这是清单,如果有人想快速尝试一下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mmo.application.listpro" android:versionCode="1" android:versionName="Development">
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:debuggable="true">
<activity
android:label="test"
android:name=".Test"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:targetSdkVersion="8" android:minSdkVersion="8" />
</manifest>
TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT
。所以你只需要创建默认的TableRow.LayoutParams,设置边距并将其应用到TextView。
TableRow.LayoutParams lp = new TableRow.LayoutParams();
lp.setMargins(2, 2, 2, 2);
row.addView(tv, lp);;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)