如何在Android Java中实现自定义导航栏,如图所示。

huangapple go评论71阅读模式
英文:

How to implement custom navigation bar in android java as shown in images

问题

以下是我想在我的应用程序中实现的底部导航栏的设计。初始状态

点击另一个图标后,我希望它显示为以下所示:点击后

我是Android新手,到目前为止,我只了解了关于底部导航栏的基本实现。我不知道如何实现这个。

英文:

Here is the design I want to implement in my app for bottom navigation bar.Initial State

After clicking another icon I want it to change as shown:After Click

I am new to android and so far I only know about the basic implementation of bottom navigation bar. I am unable to understand how to implement this.

答案1

得分: 1

你有两个选项。要么完全自己从头开始构建小部件,要么使用已有的布局和小部件来构建它。

构建自己的小部件
根据你想要实现的内容,创建自己的小部件可能会很具有挑战性。你需要创建一个扩展了 View 或其它子类的类。例如,你可以扩展自 LinearLayout,因为它可能提供了你正在寻找的一些功能。
至少需要:

  • 重写触摸事件以创建自己的触摸处理逻辑
  • 重写绘制方法以绘制自定义界面
  • 重写布局和测量方法以在调整大小时调整布局行为

这可能变得非常复杂,所以我不建议初学者这样做。虽然它允许对小部件进行完全定制,但也会带来更高的自身错误或意外行为的风险。

使用布局和小部件
使用现有的小部件和布局创建这样的小部件很容易,但在实现方面有更多限制。你可以在一个单独的布局 XML 文件中创建小部件的外观。对于功能部分,你可以编写某种 UI 控制器类,该类实现了布局所需的所有功能,然后可以将其连接到布局中(使用视图的现有回调)。

  • 在布局中使用 ID 来访问应具有特殊行为的不同组件
  • 实现点击监听器或触摸监听器来处理特定的交互事件
  • Android 提供了用于动画的 API
  • 将你的小部件的布局 XML 包含在任何其他布局 XML 中,或者单独进行填充。然后添加你的 UI 控制器。

Android 已经提供了广泛的布局和小部件,可以用来实现你想要的功能。在尝试编写自己的内容之前,我建议你利用这些已有资源。

英文:

You have two options. Either you build the widget your self completely from scratch or you use already existing layouts and widgets to construct it.

Building your own widget
Creating your own widget can be quiet challenging depending on what you are trying to accomplish. You would have to create a class that extends View or any other subclass of it. For instance you might extend from LinearLayout since it might provide some of the functions you are looking for.
You'd have to at least

  • overwrite the touch-events to create your own touch-handling
  • overwrite the draw-methods to draw your custom ui
  • overwrite the layout and measure-methods to adjust the layouts behavior when resizing etc.

This can become very complex, so i wouldn't recommend it for a beginner. While it allows for full customization of your widget it also comes with a higher risk of self inflicted bugs or unwanted behavior.

Working with Layouts and Widgets
Creating a widget like this using existing widgets and layouts is easy but has more limitations in what you an achieve. You would create a separate layout xml file in which you create the visuals of your widget. For the functional part you would write some kind of ui-controller class that implements all the required functions etc. for that layout and can be hooked up to it (using the existing callbacks of the views)

  • Use IDs in your layout to access the different components that should have special behavior
  • Implement a clicklistener or a touchlistener to handle the specific interaction events
  • For Animations Android provides an API.
  • Include your widgets layout xml into any other layout xml or inflate it separately. Then add your ui-controller

Android already provides a wide range of layouts and widgets that can be used to accomplish what you are looking for. I would utilize those before trying to write my own

huangapple
  • 本文由 发表于 2020年10月27日 01:30:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64542102.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定