Android: Creating a parent layout and extending it

2020-07-05 05:46发布

问题:

I am writing an app in which I have mutiple screens which I want to adhere to a certain theme. For example: all screens should have the same background, all screens should have a bar on the top with the same set of actions (not necessarily dependent on the activity currently being show) and all screens should share the same fonts. Is there a way in android to create a parent layout which all other layouts extend and therefore if I need to change some elements (say the background color) I won't have to do it on each individual screen but rather only in the parent screen. I guess what I'm asking is, is there a sort of layout inheritance in android?

Thanks, e.

回答1:

There is no layout inheritance but you can create this bar layout in a separate file and use include to include it in the layouts that you want to have the bar.

Some of the other stuff could also be done with styles. Create a style for different elements and then add the style to the element in the layouts.

To do inheritance you can create a layout that you can include in other layouts. Then in a BaseActivity you can modify views, add event listeners, etc for the included layout. Just make sure in your onCreate to call setContentView() before you call super.onCreate().