【问题标题】:How to add header and Footer to each activity in android如何为android中的每个活动添加页眉和页脚
【发布时间】:2012-05-04 23:11:35
【问题描述】:

我想在顶部和底部的每个活动中添加ImageButtonbuttontextview。我想使用header and footer。所以我想在我的每个 Android Activity 中添加页眉和页脚。我不知道该怎么做。我不需要如何编写页眉或页脚的源代码。我想知道的是我必须在哪里定义页眉和页脚意味着我需要在每个 xml 文件中添加页眉和页脚还是需要定义两个 header.xmlfooter.xml 并在每个文件中使用这些 xml 文件其他 xml 文件。或者是否有任何其他方式意味着使用该活动的 java 文件中的引用。任何帮助表示赞赏。

【问题讨论】:

    标签: android xml header footer


    【解决方案1】:

    “我是否需要定义两个 header.xml 或 footer.xml 并在每个其他 xml 文件中使用这些 xml 文件”

    是的,据我所知,这是最好的方法。您可以使用 include xml 标签将其他 .xml 布局文件包含在其他布局文件中。喜欢:

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

    【讨论】:

    • 谢谢你的帮助,现在我会努力的:)
    • 页眉页脚是否必须使用相对布局
    【解决方案2】:

    你有两个选择。包括和合并。

    请阅读更多关于这些选项here for includehere for merging

    【讨论】:

    • 谢谢,但我想我会选择包含,是否必须对页眉页脚使用相对布局?
    • 您可以选择适合您的页眉/页脚要求的任何布局。
    【解决方案3】:

    查看此链接:

    这和你的问题一模一样。如果你想拥有这些页眉和页脚,你应该构建一个自定义视图并在你的应用程序中使用它。您可以使用操作栏之类的内容作为标题。

    【讨论】:

    • 谢谢,但我已经检查过了。 :) 仍然感谢您的帮助
    【解决方案4】:

    Android 本身没有页眉和页脚的概念。但是,您可以在布局中定义概念性页眉和页脚一次,然后在其他布局中多次使用它们,只需使用(例如)调用它们:

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

    您可以看一下这个示例,以更好地了解如何在整个应用程序中重用布局。

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

    【讨论】:

    • 谢谢你的帮助,现在我会努力的:)
    • 页眉页脚是否必须使用相对布局
    • 不。这只是一个例子。我通常使用RelativeLayout(它们比LinerLayouts 更好)。我也在 LinearLayouts 中使用过它们。但我想,为了更快地理解这个例子,他们使用了RelativeLayout。
    • 如果你不介意的话,你想告诉我线性布局和相对布局之间的区别吗?或者为什么相对布局更好?
    • 在 LinerLayouts 中,属于布局一部分的所有元素都彼此相邻布置。您可以操纵位置的唯一方法是设置诸如方向之类的属性。另一方面,Relativelayouts 将元素彼此“相对”放置,恕我直言,提供了无与伦比的灵活性。例如,通过此链接developer.android.com/resources/tutorials/views/…,通过以下简单属性,将一个 ID 为“@+id/entry”的 EditText 放置在一个 ID 为“@id+/label”的 TextView 下方:android:layout_below="@id/标签”
    【解决方案5】:

    定义两个单独的文件header.xmlfooter.xml 并使用

    `

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

    【讨论】:

      【解决方案6】:
      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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-06
        • 2011-11-27
        • 2014-11-17
        • 2014-08-17
        • 2022-06-11
        • 1970-01-01
        相关资源
        最近更新 更多