2010年8月9日月曜日

実行時に生成したViewにstyleを適用

簡単な方法は発見できず。
代替案は、生成したいView用のlayout.xmlを用意し、LayoutInflaterでxmlからViewを生成する。

res/values/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Cell">
        <item name="android:background">#CDBA96</item>
        <item name="android:textColor">#FFFAF0</item>
        <item name="android:textSize">15dip</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:gravity">center</item>
    </style>
</resources>
res/layout/row.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView style="@style/Cell" android:text="Cell1" />
    <TextView style="@style/Cell" android:text="Cell2" />
</TableRow>
Activity.java
LayoutInflater li = getLayoutInflater();
li.inflate(R.layout.row, (ViewGroup) findViewById(R.id.parent), true);

1 件のコメント:

  1. 別の方法
    http://ym02.blogspot.com/2010/08/viewstyle_18.html

    返信削除