开发者

Should I put all Android view attributes into the style?

I've seen that some Android devs promote this form of decla开发者_运维问答rative layout:

<TextView style="@style/my_textview_style" />

i.e. that pretty much all the attributes (including the ID) go into the style definition. It certainly makes for shorter layout files, but is it a step too far?

What would you recommend goes in the style definition, and what should stay in the layout, and why?

I've started doing Android dev this year, but have prior experience of development. Issues like this are so important, the "way" we use the tools, the habits, to be more efficient. I appreciate you sharing your views on what works for you, for clarity, for efficiency, for maintainability, for app reliability.


I've pondered this very question and it becomes much more pertinent when dealing with multiple device sizes and densities.

Putting too much information into the styles can limit their use, especially when dealing with landscape and portrait layouts. For example a landscape layout may in fact be wholly different in composition but require very similar styles.

In these cases it helps to use the . notation to ammend style differences, for example.

<MyPage>
<MyPage.portrait>
<MyPage.landscape>

See below for information on the above approach to creating styles. http://developer.android.com/guide/topics/ui/themes.html

MyPage defines the common properties for your layout and you then override these values in the portrait and landscape extensions.

I..e you might have more right and left padding in landscape than portrait.

The problem i've found is that the themes and styles xml files get huge, especially if like me you have many activities, and if you address lots of creative changes then you can end up with lots of messed up definitions.

At this point you have to take stock, tidy up and re-factor.

My only advice is that you plan really well upfront. Define a naming system that accommodates your project, write it down and stick to it.

Possible style naming convention

<section>_<componentName>_<componentType>_<orientation>

home_leftPanel_btn_horizontal
home_leftPanel_listview
home_leftPanel_listview_horizontal

A similar approach to themes should be adopted.

I'm still working this out, so can't give fuller examples.


Pushing those things out into the style is a good practice if you plan on skinning your application differently. If you plan on releasing two versions with just a different look then by all means push those into a style. As you say it cleans up your layout files quite a bit.

Should you push IDs into styles. No because IDs are the hooks by which you're code will grab references to those objects. I can't see much of a point to always having your program externalize IDs. So I'd say that practice is going too far.

Now should you do this every time? Well I think that really depends on if you think you need the benefit this brings to reskin the application. Some people can't say no to a good idea, and try and do every good idea all the time. While that's admirable it's not productive. Experience is knowing what good ideas you need and what good ideas are just fluff for this application your are working on right now. To keep ourselves productive we need only the good ideas that make us move faster, and all other ideas are just art. How do I choose? Refactor, refactor, refactor. Do it fast, then refactor to clean it up based on the next requirements I'm working on. Do it fast, then refactor. Repeat until my boss smiles.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜