0

我试图让我的游戏在所有布局中都同样好用,为了促进这一点,我使用了依赖于大小的样式。问题是,由于它的高重复性,这与我直接编码的功能不同。那么,如何将我的一种样式应用于我在代码中创建的这些文本视图?

示例样式代码:

<style name="medText">
    <item name="android:textSize">22sp</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_width">wrap_content</item>
</style>

示例程序代码:

TableRow row=new TableRow(this);
TableLayout tlayout=new TableLayout(this);
text1.setText(R.string.name);
row.addView(text1);
text2.setText(R.string.level);
row.addView(text2);
text3.setText(R.string.score);
row.addView(text3);
tlayout.addView(row);

我循环重新创建这些 TextViews 多次,后面的包含变量输入。

4

2 回答 2

1

试试这个:

TableRow row=new TableRow(this);
TableLayout tlayout=new TableLayout(this);
text1.setText(R.string.name);
text1.setTextAppearance(this, R.style.medText);
row.addView(text1);
text2.setText(R.string.level);
text2.setTextAppearance(this, R.style.medText);
row.addView(text2);
text3.setText(R.string.score);
text3.setTextAppearance(this, R.style.medText);
row.addView(text3);
tlayout.addView(row);

让我知道它是否有效:从未使用过文本外观功能。

于 2011-06-02T04:31:56.767 回答
0

在 TextView 中设置样式

如果您已经在该 xml 中声明了您的 TextView,则必须通过style="@style/medText"

例子

<TextView
 android:layout_width="fill_parent"
 android:text="@string/menu_legal_termsandcondition"
 android:padding="3dip" 
 style="@style/medText"
 android:layout_weight="2"/>

如果您已经动态创建了 TextView,那么您必须text1.setTextAppearance(this, R.style.medText);在您的 java 代码中使用

谢谢迪帕克

于 2011-06-02T04:33:27.050 回答