开发者

reading android style xml

I have a custom style that I would like to apply to my views. I was not able to create my views programatically, not through XML, as it is a dynamic content amount (same view can appear X times, but no more then 10, so I do not want a list).

if i can get an XmlPullParser of the style, I can easly use AttributeSet attrs = Xml.asAttributeSet(parser); and pass it to the View's C'tor. The problem is obtaining the XmlPullParser开发者_如何转开发. getresources().getX does not have a getStyle() method. Maybe I'm just taking the wrong approach. Does anybody have any idea how to apply the style to a programatic view?

10x.

ok, i tried the third C'tpr parameter approach:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView tv = new TextView(this, null, R.style.my_style_2);
    tv.setText("this should be green!");
    setContentView(tv);
}

and the style:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="my_style_2">
        <item name="android:textColor">#FF00FF00</item>
        <item name="android:textSize">20sp</item>
    </style>
</resources>

I don't get big green text. I get default size gray text...


Ok. Other way would be just create the View programmatically and use the style parameter within the constructor:

E.g: TextView myTextView = new TextView(context, null, R.style.myCustomStyle);


I hope, this answer can help: I would define the view, which can appear x times, in a separate layout file and use the layout inflater to add them dynamically / programmatically.

Sample code:

LayoutInflater l = getLayoutInflater(); // is a method from activity class
for (int i = 0; i < x; i++)
{
    l.inflate(R.layout.custom_list_view_entry,  parentView);
}

custom_list_view_entry.xml in the res/layout folder define your list entry layout and contain all style information or a more complex view hierarchy. parentView is the view, which is the container for the list entries.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜