在code机器人定式 [英] android set style in code

查看:163
本文介绍了在code机器人定式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用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.

这篇关于在code机器人定式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆