Android Template Templates

Hi, I am working on android.

I have a layout that was used in every action.

I mean, I have a layout with a footer and a header.

In each action, the header and footer are the same and have the same actions.

As you see,

I want to use a common layout for the header and footer.

I mean in action, I put the layout of the content area in the overall layout.

I find something but not enough.

How can i do this?

Is there a dummy document for this?

sorry for bad english.

+4
source share
4 answers

What you're talking about is the new Android design template called Fragments. Since 3.0 fragments are small activities, such as presentations, which can be combined to form a screen.

So, you have to create a fragment of the header and footer, and then include them in all the actions that require them.

Another template that you might want to see is the Action bar template, which is used to place a bar at the top of screens with common content and functions, similar to your title.

Another way is to use xml files to define the header and footer, and then create them as representations in the code and add them programmatically to the definition of the xml representation of the content. The problem is that the code behind the header and footer must be replicated in each controller. It’s best to use Fragments, I will add some useful links below:

http://developer.android.com/guide/topics/ui/actionbar.html

http://developer.android.com/guide/topics/fundamentals/fragments.html

http://mobile.tutsplus.com/tutorials/android/android-compatibility-working-with-fragments/

https://stackoverflow.com/questions/5710573/need-a-fragments-example

+4
source

You can use include for the header and footer, or dynamically add them from the base class, but I think the best approach is to use a single action to place the application and then use snippets for your screen content.

http://android-developers.blogspot.co.uk/2011/02/android-30-fragments-api.html

+2
source

I have nothing against fragments, and yes, this is the way to go, but for a novice Android developer, you can achieve what you are trying to do with <include> and basic actions.

This article perfectly explains the use of <include> s, but to summarize, you can have an XML layout file that you can "include" in another layout, rather than rewriting the same stuff over and over again.

For the functionality of the headers and footers (assuming that they do something when clicked), you can create a basic activity that you can extend instead of the usual android Activity . Define the logic for the header and footer clicks in this basic action, for example, using this code example:

 public class MyBaseActivity extends Activity { ... public void onHeaderClick(View view) { // when header is clicked, do this. } public void onFooterClick(View view) { // when footer is clicked, do this. 

In your layout (the one that you have as a separate xml) add the onClick attribute to your header / footer by assigning the method name in the base action.

such as

 android:onClick="onHeaderClick" 

Then it's just a matter of expanding MyBaseActivity for all of your actions that have headers and footers.

+1
source

Check this one , you can really reuse your layout whenever you want.

-1
source

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


All Articles