我想在我的Android应用程序中构建一个动态表。我希望有一个70%宽的专栏和一个30%宽的专栏。以编程方式应用样式
如果我采用了android的布局XML做到这一点:layout_weight我没有任何问题:
<TableLayout
android:id="@+id/TableLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:background="#ffffff" >
<TableRow
android:id="@+id/TableRow4"
style="@style/RigaTabella"
android:background="#d2ef7b" >
<TextView
android:id="@+id/TitoloSpesaTabella"
style="@style/Titolo"
android:layout_width="0px"
android:layout_weight="0.7"
android:background="#cdcdcd"
android:text="@string/spesa" />
<TextView
android:id="@+id/TitoloCostoTabella"
style="@style/Titolo"
android:layout_width="0px"
android:layout_weight="0.3"
android:background="#ababab"
android:text="@string/costo" />
</TableRow>
</TableLayout>
这是我获得:http://i.imgur.com/DHpXBzJ.jpg
(我用这个灰色背景显示两个TextView中的实际尺寸)
但是,如果我尝试使用此代码:
TableLayout TableLayout = (TableLayout) findViewById(R.id.TableLayout);
for (int i = 0; i < 10; i++) {
TableRow Row = (TableRow) getLayoutInflater().inflate(R.layout.table_row_style, null);
if (i % 2 == 0)
Row.setBackgroundColor(Color.parseColor("#ffffff"));
else
Row.setBackgroundColor(Color.parseColor("#f1f1f1"));
TextView tv = (TextView) getLayoutInflater().inflate(R.layout.testo_sx_style, null);
tv.setText("Test Test");
TextView tv2 = (TextView) getLayoutInflater().inflate(R.layout.testo_dx_style, null);
tv2.setText("$ 1000");
Row.addView(tv);
Row.addView(tv2);
TableLayout.addView(Row);
}
其中testo_sx_style.xml是:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:gravity="left"
android:textSize="13sp"
android:textColor="#161616"
android:typeface="monospace"
android:background="#cdcdcd"
/>
,并在那里testo_dx_style.xml是:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:gravity="left"
android:textSize="13sp"
android:textColor="#161616"
android:typeface="monospace"
android:background="#ababab"
/>
我获得此:http://i.imgur.com/oe6YHsz.png
我真的不知道是什么错误。例如,我也尝试在testo_sx_style.xml中将layout_width设置为100px,但不会更改任何内容。似乎有些属性不适用于我的代码。
任何人都知道什么是错误?提前致谢。
祝您有美好的一天:)
ps.s.对不起,我的英语
不起作用。但是我找到了解决方案!看看我的回答:)感谢您的帮助:) –