How to extend the include tag on Android

As a result, I got a lot of xml files that have the same code in the header and footer. Is there a good way to create some kind of template and pass it a resource for inclusion in the middle?

In other words, how to expand the Include tag so that I can, and not just include the view, include a template that includes this resource?

My dirty code:

<?xml version="1.0" encoding="utf-8"?> <include layout="@layout/settings_section" /> 

settings_section.xml:

 <?xml version="1.0" encoding="utf-8"?> <!--Header Begin--> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="10dp" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@layout/my_shape" android:orientation="vertical" ><include layout="@layout/header" /> <!--End header--> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:text="Blah blah blah" /> <!--Footer Begin --> </LinearLayout> </LinearLayout> <!-- Footer End --> 

What I would like:

 <include layout="@layout/header" /> <com.example.Include layout="@layout/settings_section" inside="@layout/default_template" /> 

EDIT : I am looking for code samples.

+4
source share
5 answers

This does not answer your question, but I used styles for similar situations.

Res / layout / layout.xml:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" style="@style/header1" > <LinearLayout style="@style/header2" > <include layout="@layout/header" /> ... 

RES / values ​​/ styles.xml

 ... <style name="header1"> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">fill_parent</item> <item name="android:orientation">vertical</item> <item name="android:padding">10dp</item> </style> <style name="header2"> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">fill_parent</item> <item name="android:background">@layout/my_shape</item> <item name="android:orientation">vertical</item> <style> ... 

In any case, this approach simplifies the parts that you need to copy / paste into multiple files, and makes them fairly simple and flexible. You can make changes to layout.xml if necessary - the values ​​you give in the styles are overwritten - and changing the style affects all layouts using them.

+2
source

Just an idea: inside settings_section xml, I would create a container (a LinearLayout ) with id. Then in Activity.onCreate() I will find this container by its id and add a child element ( default_template ) to it.

Then I would create some kind of YourBaseActivity with the protected method addContent(int contentId) , so the code to find the container and paste the content into it does not repeat in other similar actions.

+1
source

If you want to describe the behavior, then this is possible - you will need to create your own view class that extends LinearLayout (see Building custom components) . When this view is created, it can receive the attributes specified in XML (see Passing user variables through XML resources ) and inflate these resources.

XML settings look like

 <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@layout/my_shape" android:orientation="vertical" ><include layout="@layout/header" /> <!--End header--> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:text="Blah blah blah" /> <!--Footer Begin --> </LinearLayout> 

Please note that I got rid of the external LinearLayout for the content, since your custom view will be a LinearLayout type anyway. If I have time, I will DoubleInclude later and send the code.

+1
source

Here is the xml template

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/myviewHeader" android:layout_height="wrap_content" android:layout_width="fill_parent" > </TextView> <LinearLayout android:id="@+id/myviewIncluder" android:layout_height="wrap_content" android:layout_width="fill_parent" > </LinearLayout> <TextView android:id="@+id/myviewFooter" android:layout_height="wrap_content" android:layout_width="fill_parent" > </TextView> </LinearLayout> 

and class for dynamic content view group

 import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; public class MyView extends ViewGroup { private LayoutInflater mInflater; private TextView header; private TextView footer; public MyView(Context context,View child) { super(context); mInflater = LayoutInflater.from(context); mInflater.inflate(R.layout.myview, this); LinearLayout includer = (LinearLayout) findViewById(R.id.myviewIncluder); header = (TextView) findViewById(R.id.myviewHeader); footer = (TextView) findViewById(R.id.myviewFooter); includer.addView(child); } @Override protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) { // TODO Auto-generated method stub } public void set_HeaderText(String text){ header.setText(text); } public void set_FooterText(String text){ footer.setText(text); } } 

If you want to add vie to the Includer part, yozu can act as follows:

  View include = null; LayoutInflater mInflater = LayoutInflater.from(this); include = mInflater.inflate(R.xml.list_item_icon_text,null); ViewGroup text = new MyView(this,include); 
+1
source

The Eclipse build system is not very flexible by default, but if you want to go with Ant for your build system, you can do much more during build, for example, to create XML files. Eclipse can use Ant to create a project instead of its own build system, as for most projects. By generating XML files at build time, rather than at run time, as suggested by @Arhimed, you can see the source of the XML file and its rendering in Eclipse to make sure it looks right. This approach is a bit more advanced, but it offers much more flexibility. When you use Ant, you can also do other things, for example, create two versions of an application with different settings, such as a paid version and a light version. Here's an Ant intro for FilterChains: http://ant.apache.org/manual/Types/filterchain.html and a document on using Ant for Android customs duties apk: http://blog.elsdoerfer.name/2010/04/29/ android-build-multiple-versions-of-a-project /

0
source

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


All Articles