Reusing Android Layouts

In the iphone context, when we add a view as a subtitle of another view, everything that happens with our view will be processed correctly, because in xib we say which class will handle its actions. How can we achieve this on android? Since we don’t have such a connection between the .xml layout and the class that uses it, how can we achieve something like this?

The main goal is that, for example, having one common header and one common footer for the entire application, we just want to add different views to the “view content” between the header and footer.

+4
source share
1 answer

You can use the include layout. This allows you to create a layout file for your header and one for the footer, and then include these layouts in the main Activity layout. And if you want to include the header + footer in several actions, and there are some in these layouts the events you want to handle, you can create a BaseActivity that handles these events, and then your other actions extend BaseActivity.

Pseudo-code example:

title.xml

<LinearLayout><ImageView/><TextView/></LinearLayout> 

footer.xml

 <LinearLayout><TextView/><TextView/></LinearLayout> 

main.xml

 <RelativeLayout> <include layout="@layout/title"/> <WebView /> <include layout="@layout/footer"/> </RelativeLayout> 
+4
source

Source: https://habr.com/ru/post/1340313/


All Articles