开发者

Creating a custom control or making buttons inline

I have a开发者_Python百科 need to create a control similar to UITabBar in iPhone, which is to be present on every activity of my application. UITabBar essentially is a battery of buttons exhibiting a TAB like behaviour: every button maps to an activity.

I have two solutions for this:

  1. In the layout XML for every activity, I insert a <LinearLayout><Button/><Button/><Button/></LinearLayout> element. And then have a common listener class that will handle the button clicks. So, every activity will have an instance of this listener.

  2. To create a custom Widget extending LinearLayout class, put all the buttons as its static members and let it handle the button clicks. Include this custom control in every screen.

I am not sure which approach to follow. Please advice. Alternatives also appreciated

Following is what I think about above approaches:

The problem with the first approach:

  1. It will generate a lot of boilerplate code(findViewByIds, setOnClickListener etc.)

  2. Assuming that there are 5 activities and 3 tab buttons, the total number of Button objects created at Runtime will be 5 x 3 = 15

I'd like to take the 2nd approach because:

  1. All the code(state and behaviour) will be encapsulated by the widget class. Less Boilerplate code.

  2. Since the buttons will be static members, the total number of Button objects created at Runtime will be only three. Though, the static members will remain in memory for a longer time(until JVM unloads the class), since the control is present on every screen I think this can be excused.

Thanks.


Why not just use the existing TabWidget implementation in Android?

http://developer.android.com/guide/tutorials/views/hello-tabwidget.html

Not only is implementing your own widget extra work, it's likely to feel out of place when compared with other apps on the platform.

In particular, here's some problems I see:

  1. When the look of the tab bar changes due to a system update, you'll have to re-write your code. You're also unlikely to exactly match the look and feel of the built in tab bar. (Think Java on OS X.)

  2. Every time a user clicks on a tab, it will add another activity to the stack. Not only is this a waste of memory, but every time the user clicks the back button on the phone, it will go to the previous tab. This isn't the way the tab bar is supposed to work.


Unfortunately, I couldn't find a way to customize the built-in TabWidget to make it look like UITabBar. So, I am going to create a custom control and have the Buttons as static members(take the second approach). The control will handle the clicks as expected.
I've notice that the award winning Plink Art application too has a tab control that is similar to iPhones UITabBar.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜