2014-02-19 99 views
0

我想在我的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.对不起,我的英语

回答

0

你可能尝试这样的事情,利用权重参数的布局:

TableRow tr1 = new TableRow(this); 
tabInfo = new TableLayout(this); 
tabInfo.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 4.0f)); 
tr1.addView(tabInfo); 
+0

不起作用。但是我找到了解决方案!看看我的回答:)感谢您的帮助:) –

1

解决了!只有这行代码:

tv.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.7f)); 

有了这个我可以设置宽度为0px,到WRAP_CONTENT高度和重量至0.7。

它很好用:)

p.s.我在这里找到了解决方案:Set the layout weight of a TextView programmatically

+0

谢谢,它也适用于我 - 我想在右侧添加一个文本(从左到右)和图像。距文字的图像距离与文字长度有关。你的线路解决了这个问题 –

相关问题