简体   繁体   中英

How to add header and Footer to each activity in android

I want to add a ImageButton , a button and a textview in each of my activity at top and bottom.I thought of using header and footer . So I want to add a header and footer in each of my Android Activity. I don't have any idea of how to do that. I don't need source code of how to write a header or footer. What i want to know is where i have to define that header and footer means do i need to add a header and footer in each xml file or do i need to define two header.xml or footer.xml and use these xml files in each of other xml files. Or is there any other way mean like using a reference from the java file of that activity. Any help Appreciated.

Define two separate files header.xml and footer.xml and and than use

`

<include layout="@layout/footer"/>

See this link :

It's exactly like your question. If you want to have these header and footer you should build a custom View and use it in your application. You can use something like action bar as your header.

"do i need to define two header.xml or footer.xml and use these xml files in each of other xml files"

Yes, as far as I know this is the best way to do it. You can use the include xml tag to include other .xml layout files in other layout files. Like:

...
<include layout="@layout/header"/>
...
<include layout="@layout/footer"/>
...

Android does not have the concept of Header and Footer per se. You could however, define the conceptual headers and footers in your layouts once and then use them many times in other layouts simply by calling them using the (for example):

<include layout="@layout/header"/>

You can give this example a look to better understand how to re-use layouts throughout your application.

http://developer.android.com/training/improving-layouts/reusing-layouts.html

You have two options. Include and Merge.

Please read more about these options here for include and here for merging

This is best example for Common Header Footer in All Activities


BaseActiivty.java
=================



 public class BaseActivity extends FragmentActivity {

    RelativeLayout mRelativeLayout;
    FrameLayout frame_container;
    TextView header_txt,footer_txt;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);


    }

    @Override
    public void setContentView(int layoutResID)

    {
        mRelativeLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_base, null);
        frame_container = (FrameLayout) mRelativeLayout.findViewById(R.id.frame_container);
        // set the drawer dialog_view as main content view of Activity.
        setContentView(mRelativeLayout);
        // add dialog_view of BaseActivities inside framelayout.i.e. frame_container
        getLayoutInflater().inflate(layoutResID, frame_container, true);

        header_txt = (TextView) findViewById(R.id.header_txt);
        footer_txt = (TextView) findViewById(R.id.footer_txt);

    }
}


   MainActivity.java
   =================

public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }





activity_base.xml
=================





<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/content_base"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <RelativeLayout
        android:id="@+id/header_RL"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/colorAccent">

        <TextView
            android:id="@+id/header_txt"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="30dp"
            android:gravity="center"
            android:text="Header"/>

    </RelativeLayout>


    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/footer_RL"
        android:layout_below="@+id/header_RL">

    </FrameLayout>


    <RelativeLayout
        android:id="@+id/footer_RL"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="@color/colorAccent">

        <TextView
            android:id="@+id/footer_txt"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="30dp"
            android:gravity="center"
            android:text="Footer"/>

    </RelativeLayout>

</RelativeLayout>





activity_main.xml
==================


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Hello World!"
            android:textSize="30dp" />

    </LinearLayout>
</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM