在应用/"permanent" textView 中的每个 activity 上显示相同的 TextView?
Display the same TextView on every activity in an app/"permanent" textView?
所以我正在制作游戏,游戏的核心方面之一(与大多数其他游戏一样)是你有多少钱。因此,在每个 activity 上,我都有一个显示玩家资金的 TextView(我有一个 "global" 静态变量设置,因此我可以从应用程序的任何位置访问玩家当前的资金)。
截至目前,我有一堆不同的 textView,其 ID 类似于 "moneycount1, moneycount2, etc.",并且在每个 activity 上,当我恢复 activity 时,我只是将 textview 设置为显示玩家的钱。
但是,有什么方法可以让 "permanent" 文本视图在应用程序的每个 activity 上出现在同一位置并显示相同的信息?这将节省大量时间和重复。
谢谢
每个 activity 必须有自己的布局,所以这可能是不可能的。我能想到的让生活更轻松的第一个选择是:
考虑使用 include 标签,将相同的组件包含在不同的布局文件中:https://developer.android.com/training/improving-layouts/reusing-layouts
你只需用你的 textview/components 创建一个布局文件,然后用作:
<include layout="@layout/yourLayoutName"/>
在你的布局中
或者,您也可以有一个包含不同片段的 activity,然后将这个全局值存储在 activity 中,这样所有片段都可以访问它或者尝试构建包含此片段的不同布局文件
看看这个:https://developer.android.com/guide/components/fragments
它说:
A Fragment represents a behavior or a portion of user interface in a
FragmentActivity. You can combine multiple fragments in a single
activity to build a multi-pane UI and reuse a fragment in multiple
activities. You can think of a fragment as a modular section of an
activity, which has its own lifecycle, receives its own input events,
and which you can add or remove while the activity is running (sort of
like a "sub activity" that you can reuse in different activities).
所以我正在制作游戏,游戏的核心方面之一(与大多数其他游戏一样)是你有多少钱。因此,在每个 activity 上,我都有一个显示玩家资金的 TextView(我有一个 "global" 静态变量设置,因此我可以从应用程序的任何位置访问玩家当前的资金)。
截至目前,我有一堆不同的 textView,其 ID 类似于 "moneycount1, moneycount2, etc.",并且在每个 activity 上,当我恢复 activity 时,我只是将 textview 设置为显示玩家的钱。
但是,有什么方法可以让 "permanent" 文本视图在应用程序的每个 activity 上出现在同一位置并显示相同的信息?这将节省大量时间和重复。
谢谢
每个 activity 必须有自己的布局,所以这可能是不可能的。我能想到的让生活更轻松的第一个选择是:
考虑使用 include 标签,将相同的组件包含在不同的布局文件中:https://developer.android.com/training/improving-layouts/reusing-layouts
你只需用你的 textview/components 创建一个布局文件,然后用作:
<include layout="@layout/yourLayoutName"/>
在你的布局中
或者,您也可以有一个包含不同片段的 activity,然后将这个全局值存储在 activity 中,这样所有片段都可以访问它或者尝试构建包含此片段的不同布局文件
看看这个:https://developer.android.com/guide/components/fragments
它说:
A Fragment represents a behavior or a portion of user interface in a FragmentActivity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).