English 中文(简体)
代码和roid体字体
原标题:setting style for textview in code, android

I have the following code in my activity:

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

这里是我的风格。

<?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>

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.xml

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

法典:

activity.getLayoutInflater().inflate(R.layout.styled_textview, <root_node>, true);
问题回答

附上您的案文。 与此类似:

layout/cell_row.xml

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/TitleRow">

YourActivity.java

......  
LayoutInflater inflater = getLayoutInflater(); // called from activity
......
DataCell[i] = inflater.inflate(R.layout.cell_row, null); 
// or inflater.inflate(R.layout.cell_row, root, false);   where root is a parent view for created TexView 
DataCell[i].setText(data[i]);
......




相关问题
Creating a Style in code behind

Does anyone know how to create a wpf Style in code behind, I can t find anything on the web or MSDN docs. I have tried this but it is not working: Style s = new Style(typeof(TextBlock)); s....

WPF Custom Themes

I have a simple question which is giving me some difficulty. I have downloaded a custom them for WPF of the net. Now i want to apply this theme to my App instead of the default one. How do i do that,...

Is it sometimes bad to use <BR />?

Is it sometimes bad to use <BR/> tags? I ask because some of the first advice my development team gave me was this: Don t use <BR/> ; instead, use styles. But why? Are there negative ...

WPF - How to apply effect to a cropped image?

I have an Image being clipped like so: <Image Width="45" Grid.Column="0" Source="{Binding Photo}"> <Image.Clip> <RectangleGeometry Rect="0,0,45,55" RadiusX="8" RadiusY="8" /...

WPF ListView : Header styling

I want to have a ListView with columns and a particular style: The background for ALL column headers should be transparent except when the mouse is over in one of them. When this happends, the ...

热门标签