创建自定义控件或使按钮内联

发布于 2024-08-15 10:51:20 字数 923 浏览 2 评论 0原文

我需要创建一个类似于 iPhone 中的 UITabBar 的控件,该控件将出现在我的应用程序的每个活动中。 UITabBar 本质上是一组按钮,表现出类似于 TAB 的行为:每个按钮都映射到一个活动。

我对此有两个解决方案:

  1. 在每个活动的布局 XML 中,我插入一个

  2. 要创建扩展 LinearLayout 类的自定义 Widget,请将所有按钮作为其静态成员并让它处理按钮单击。在每个屏幕中包含此自定义控件。

我不确定该遵循哪种方法。请指教。替代方案也受到赞赏

以下是我对上述方法的看法:
第一种方法的问题:

  1. 它将生成大量样板代码(findViewByIds、setOnClickListener 等)

  2. 假设有 5 个活动和 3 个选项卡按钮,则运行时创建的 Button 对象总数将为 5 x 3 = 15

我想采用第二种方法,因为:

  1. 所有代码(状态和行为)都将由小部件类封装。更少的样板代码。

  2. 由于按钮将是静态成员,因此在运行时创建的 Button 对象总数将仅为三个。不过,静态成员将在内存中保留更长的时间(直到 JVM 卸载该类),因为该控件出现在每个屏幕上,我认为这是可以原谅的。

谢谢。

I have a 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赴月观长安 2024-08-22 10:51:20

为什么不直接使用 Android 中现有的 TabWidget 实现呢?

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

实现您自己的小部件不仅需要额外的工作,而且与平台上的其他应用程序相比,它可能会感觉格格不入。

特别是,我看到了一些问题:

  1. 当选项卡栏的外观由于系统更新而发生变化时,您将不得不重新编写代码。您也不太可能与内置选项卡栏的外观和感觉完全匹配。 (想想 OS X 上的 Java。)

  2. 每次用户单击选项卡时,它都会向堆栈添加另一个活动。这不仅浪费内存,而且每次用户单击手机上的后退按钮时,都会转到上一个选项卡。这不是选项卡栏应有的工作方式。

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.

对你而言 2024-08-22 10:51:20

不幸的是,我找不到一种方法来自定义内置的TabWidget,使其看起来像UITabBar。因此,我将创建一个自定义控件并将按钮作为静态成员(采用第二种方法)。该控件将按预期处理点击。
我注意到屡获殊荣的 Plink Art 应用程序也有一个类似于 iPhone 的 UITabBar 的选项卡控件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文