在code机器人定式定式、机器人、code

由网友(繁花盛开终究不过梦一场)分享简介:我试图用TextView的构造与风格是这样的:I'm trying to use the TextView constructor with style like this:会将myText的TextView =新的TextView(MyActivity.this,空,R.style.my_style);TextVi...

我试图用TextView的构造与风格是这样的:

I'm trying to use the TextView constructor with style like this:

会将myText的TextView =新的TextView(MyActivity.this,空,R.style.my_style);

TextView myText = new TextView(MyActivity.this, null, R.style.my_style );

然而,当我做到这一点,文中观点似乎并没有采取风格(我用一个静态对象设置验证它的样式)。

however, when i do this, the text view does not appear to take the style (I verified the style by setting it on a static object).

我也试着使用 myText.setTextAppearance(MyActivity.this,R.style.my_style),但它也不能正常工作

I've also tried using myText.setTextAppearance(MyActivity.this, R.style.my_style) but it also doesn't work

推荐答案

我不相信你可以编程设置样式。为了解决这个问题,你可以创建一个指定的样式模板布局xml文件,例如在RES /布局创建tvtemplate.xml与以下内容:

I do not believe you can set the style programatically. To get around this you can create a template layout xml file with the style assigned, for example in res/layout create tvtemplate.xml as with the following content:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is a template"
        style="@style/my_style" />

再膨胀这个实例化新的TextView:

then inflate this to instantiate your new TextView:

TextView myText = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null);

希望这有助于。

Hope this helps.

阅读全文

相关推荐

最新文章