开发者

overriding a style in android

开发者 https://www.devze.com 2023-01-20 18:28 出处:网络
Let\'s say i have a layout with style: <LinearLAyout ... > <TextView ... style=someStyle />

Let's say i have a layout with style:

<LinearLAyout ... >
    <TextView ... style=someStyle />
    <ImageView ... style=som开发者_如何学编程eImageStyle ... />
</LinearLayout>

the style will be defined in an xml in my project. How can i override that style with an external xml ? (i'm asking because i've noticed the View does not have applyStyle\setStyle or anything of that sort (best bet, because style need to be parsed, compared against android:attr for validation and then applied on each item of the view). I do wonder how am i suppose to make downlaodable themes for my app.


After some experience i can share my thoughts about it here in my own question... So first remark is, even in your own styles, use Dimentions and colors and attributes as much as you can. this way if you ever want to programatically apply a style to a view you can use getResources.getDimen... , getRessources().getColor(), getResrouces.getDrawable() etc...

In addition you can take the code from the aapt or open an existing apk and take the compiled xml from there and then use the same code reading the xml (it's C code mind you!!!) from android source. i would not do it from the simple reason it's an overkill to apply a simple style to a view. The time it takes to write the method is too short. and if you must do it in xml, you can create an xml file with just your view and then inflate it, you can't replace the style in run time though. for that you have to define your own mechanism to replace colors, sizes, backgrounds etc... and supply the images as well, which is not so easy, reading those images from the local storage, if i'm not mistaken is less productive then readin them from assets or drawable directories.

0

精彩评论

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