如何在活动之间共享通用布局而没有片段

编程入门 行业动态 更新时间:2024-10-21 09:09:14
本文介绍了如何在活动之间共享通用布局而没有片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有可能在活动之间共享布局(部分)?例如,在我的应用程序中,所有活动的布局都相似,顶部是长操作指示器(进度条,未执行任何操作时将其隐藏),底部用于显示错误.对于所有活动,只有中间部分是不同的.参见下面的图片.

Is there any possible way to share layout(part) between activities? For example, in my app, all activities have similar layout, the top part is long operation indicator (a progress bar, hidden when no operation is being executed), the bottom part is for showing errors. Only the middle part is different for all activities. See the picture below.

所以我的问题是,是否可以为我的应用程序中的所有活动重用通用布局(加载和错误部分)? (目前,出于某些原因,我不想使用片段进行操作)

so my question is, is it possible to reuse the common layout(loading and error part) for all activities in my app? (currently I don't want to use fragment to do it for some reasons)

也许布局资源应该是这样的:

maybe the layout resources should like this:

布局文件夹

activity_common.xml activity_one_content.xml activity_two_content.xml

谢谢

推荐答案

您可以创建一个抽象的基础"活动,所有活动都从该活动扩展而来,覆盖setContentView以合并基础和子活动布局.

You can create an abstract 'base' activity that all your activities extend from, overriding setContentView to merge the base, and sub activity layouts.

通过这种方式,您可以处理基本活动中的所有加载/错误代码,并且只需在子活动中隐藏和显示视图之间切换即可.

This way you can handle all the loading/error code in the base activity, and simply toggle between hiding and showing the views in the sub activities.

抽象活动:

public abstract class BaseActivity extends Activity { protected RelativeLayout fullLayout; protected FrameLayout subActivityContent; @Override public void setContentView(int layoutResID) { fullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_base, null); // The base layout subActivityContent = (FrameLayout) fullLayout.findViewById(R.id.content_frame); // The frame layout where the activity content is placed. getLayoutInflater().inflate(layoutResID, subActivityContent, true); // Places the activity layout inside the activity content frame. super.setContentView(fullLayout); // Sets the content view as the merged layouts. } }

布局文件:

<RelativeLayout xmlns:android="schemas.android/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- The main content view --> <FrameLayout android:id="@+id/loading_frame" android:layout_width="match_parent" android:layout_height="wrap_content" /> <!-- The main content view --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <FrameLayout android:id="@+id/error_frame" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout>

更多推荐

如何在活动之间共享通用布局而没有片段

本文发布于:2023-08-01 12:30:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1268359.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:片段   布局   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!