在code设置样式的TextView,机器人机器人、样式、code、TextView

2023-09-07 09:13:33 作者:我的男神是个帅比

我有以下的code在我的活动:

I have the following code in my activity:

......  
DataCell[i] = new TextView(context,null,R.style.TitleRow);
DataCell[i].setText(data[i]);
......

下面是我的style.xml文件,该文件是在res >>值文件夹:

Here is my style.xml file, which is in res >> values folder:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="TitleRow" parent="@android:style/TextAppearance.Medium">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#A0A0A0</item>
        <item name="android:layout_marginTop">5dp</item>
        <item name="android:layout_marginBottom">5dp</item>
        <item name="android:paddingLeft">2dp</item>
        <item name="android:paddingRight">2dp</item>
    </style>
</resources>

是越来越不显示值..但是,如果使用 DATACELL [I] =新的TextView(上下文),其工作的罚款。我知道有一些问题,我使用空的属性设置的事实。但寻找了很长时间后,我无法找到如何做一个完美的例子。我希望有人能够一劳永逸澄清这一点给我。

No value is getting displayed.. But if I use DataCell[i] = new TextView(context) , its working fine. I know there is some problem with the fact that I am using null for attribute set. But after searching for a long time I am unable to find a perfect example of how to do it. I hope someone could clarify this to me once and for all.

推荐答案

我通常最终会创造只含我想要实例(TextView的在你的情况下)元素的简单布局文件已应用正确的风格和使用的布局吹气为我创建实例。

I usually end up creating a simple layout file only containing the element I want to instantiate ( TextView in your case ) that has the proper style applied and use the layout inflater to create the instance for me.

布局文件:styled_textview.xml

layout file: styled_textview.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView ... style="@+style/MyStyle" ... />

code:

activity.getLayoutInflater().inflate(R.layout.styled_textview, <root_node>, true);