以编程方式应用样式

时间:2014-02-19 16:59:18

标签: android dynamic styles

我正在尝试在Android应用程序中构建动态表。我希望有一个70%宽的色谱柱和一个30%宽的色谱柱。

如果我使用android:layout_weight布局xml,我没有任何问题:

    <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,但它不会改变任何内容。似乎某些属性不适用于我的代码。

任何人都知道错误是什么?提前谢谢。

祝你有个美好的一天:)

P.S。对不起我的英文

2 个答案:

答案 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

答案 1 :(得分: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);
相关问题