2012-04-24 50 views
3

我想在上面和下面的每個活動中添加ImageButtonbuttontextview。我想使用header and footer。所以我想在我的每個Android活動中添加一個頁眉和頁腳。我不知道如何做到這一點。我不需要如何編寫頁眉或頁腳的源代碼。我想知道的是我必須在哪裏定義頁眉和頁腳意味着是否需要在每個xml文件中添加頁眉和頁腳,或者是否需要定義兩個header.xmlfooter.xml並在其他每個xml文件中使用這些xml文件。或者是否有其他方式意味着像使用該活動的java文件中的引用。任何幫助讚賞。如何添加頁眉和頁腳到Android的每個活動中

回答

9

定義兩個單獨的文件header.xmlfooter.xml和比使用

`

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

「我需要定義兩個header.xml或footer.xml和其它各xml文件,使用這些XML文件」

是的,據我所知,這是做到這一點的最好辦法。您可以使用include xml標籤在其他佈局文件中包含其他.xml佈局文件。如:

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

感謝您的幫助,現在我會盡力爲:) – 2012-04-24 10:21:19

+0

是必須使用頁眉頁腳的相對佈局 – 2012-04-24 10:22:26

1

您有兩種選擇。包含和合並。

請閱讀更多有關這些選項here for includehere for merging

+2

謝謝,但我想我會去包括,是否必須使用相對拉你爲頁眉頁腳? – 2012-04-24 10:23:18

+0

您可以選擇適合頁眉/頁腳要求的佈局。 – krishnakumarp 2012-04-24 10:27:08

4

請參閱此鏈接:

這正是像你的問題。如果你想擁有這些頁眉和頁腳,你應該建立一個自定義的View並在你的應用程序中使用它。你可以使用諸如動作欄之類的東西作爲標題。

+0

謝謝,但我已經檢查過了。 :)仍然感謝您的幫助 – 2012-04-24 10:27:05

2

Android本身不具有頁眉和頁腳的概念。但是,您可能,定義概念頁眉和頁腳的佈局一次,然後用它們在其他佈局多次簡單地通過使用(例如)美其名曰:

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

您可以舉這個例子來看看,以便更好地瞭解如何在整個應用程序中重新使用佈局。

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

+0

感謝您的幫助,現在我將嘗試:) – 2012-04-24 10:21:13

+1

是否必須使用頁眉頁腳的相對佈局 – 2012-04-24 10:22:21

+0

不可以。這只是一個例子。我通常使用RelativeLayout(它們比LinerLayouts更好)。我也在LinearLayouts中使用過它們。但我想,爲了更快地理解示例,他們使用了RelativeLayout。 – 2012-04-24 10:26:28

1
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> 
相關問題