有没有可能在活动之间共享布局(部分)?例如,在我的应用程序中,所有活动的布局都相似,顶部是长操作指示器(进度条,未执行任何操作时将其隐藏),底部用于显示错误.对于所有活动,只有中间部分是不同的.参见下面的图片.
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>更多推荐
如何在活动之间共享通用布局而没有片段
发布评论