【问题标题】:How do I create a header or footer button bar for my Android application如何为我的 Android 应用程序创建页眉或页脚按钮栏
【发布时间】:2011-12-11 11:25:32
【问题描述】:

Google Maps、Facebook、Foursquare 等许多流行应用的大部分活动都有页眉和/或页脚栏。这些标题通常包含非常有用的按钮,并且 我想为我的应用创建一个。有谁知道他们是怎么做的?到目前为止我还没有找到任何东西。

以下是我的意思的一些图片: http://www.flickr.com/photos/calebgomer/6262815430 http://www.flickr.com/photos/calebgomer/6262815458

【问题讨论】:

    标签: android xml android-layout header footer


    【解决方案1】:

    只需根据需要创建一个 xml。

    使用您的其他屏幕 XML 中的标签将这些 XML 添加到您的其他屏幕。

    Sample is here.

    您可以在每个活动中根据需要处理按钮单击。

    希望这会有所帮助。

    【讨论】:

    • 我以前在android上工作过。我有工作应用程序,我只是希望他们的 GUI 看起来更好。我会看看这些样本,看看我能做什么。谢谢!
    • @Vinay 不要只提供链接,此链接已损坏。请更新您的链接。
    【解决方案2】:

    设计带有页眉、页脚和正文的 XML 布局。每次都必须扩展主要活动,必须通过膨胀所需的布局来更改正文。

    【讨论】:

      【解决方案3】:

      我认为您在这些布局中喜欢的元素是它们在显示的下部使用 FrameLayout。

      他们可能正在做这样的事情:

      <LinearLayout android:orientation="vertical
        .../>
       <head layout element>
       <FrameLayout element>
       <footer layout element>
      </LinearLayout>
      

      【讨论】:

      • 使用 LinearLayout 页脚和使用 FrameLayout 作为页脚有区别吗?
      • 框架布局不是页脚。它是框架布局的内容窗格。我已将答案编辑得更具体。
      • 我明白你的意思。我将“body”从 LinearLayout 更改为 FrameLayout,但我没有看到两者之间的区别。在这种情况下使用 FrameLayout 有实际优势吗?
      • 我不知道。我认为它们看起来更好。
      【解决方案4】:

      使用这种方式,您可以创建您的 header-footer xml 并将其用于您的任何 activity 并且您只需在 HeaderFooter 中的页眉页脚中为控件编写代码 .java 并且可以在您的项目中访问它。

      构建您的 HederFooter.xml

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical" android:layout_width="fill_parent"
          android:layout_height="fill_parent" android:weightSum="10"
          android:id="@+id/commonlayout" android:background="#FFFFFF">
          <LinearLayout android:id="@+id/llheader"
              android:layout_width="fill_parent" android:layout_height="0dp"
              android:background="@drawable/bar" android:layout_weight="1">
      
      
      
              <RelativeLayout android:id="@+id/relativeLayout1"
                  android:layout_width="fill_parent" android:layout_height="fill_parent"
                  android:layout_gravity="center">
                  <Button
                      android:id="@+id/Button_HeaderFooterSubscribe"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_alignParentRight="true"
                      android:layout_marginRight="2dp"
                      android:layout_centerVertical="true"
                      android:background="@drawable/subscribe"
                      />
                      <Button
                      android:id="@+id/Button_logout"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_alignParentRight="true"
                      android:layout_marginRight="2dp"
                      android:layout_centerVertical="true"
                      android:background="@drawable/logout"
                      />
                      <Button
                      android:id="@+id/Button_playlist"
                      android:layout_marginLeft="2dp"
                      android:layout_width="wrap_content"
                      android:layout_centerVertical="true"
                      android:layout_height="wrap_content"
                      android:layout_alignParentLeft="true"
                      android:background="@drawable/tempadd"
                      />
      
      
      
      </RelativeLayout>
      
          </LinearLayout>
          <LinearLayout android:id="@+id/lldata"
              android:layout_weight="8" android:layout_width="fill_parent"
              android:layout_height="0dp" android:background="#FFFFFF">
      
      
          </LinearLayout>
      
          <LinearLayout android:id="@+id/llfooter"
              android:layout_weight="1" android:layout_width="fill_parent"
              android:orientation="horizontal" android:layout_height="0dp"
              android:visibility="visible" android:background="@drawable/fbg"
              android:weightSum="5.0" android:gravity="center"
              android:layout_margin="0dp">
      
              <Button android:id="@+id/home" android:layout_height="wrap_content"
                  android:layout_width="wrap_content" android:layout_weight="1"
                  android:background="@drawable/home" android:textColor="#FFFFFF"
                  android:padding="10px"></Button>
      
              <Button android:id="@+id/issue" android:layout_height="wrap_content"
                  android:layout_width="wrap_content" android:layout_weight="1"
                  android:background="@drawable/issue" android:textColor="#FFFFFF"
                  android:padding="10px"></Button>
      
              <Button android:id="@+id/browse" android:layout_height="wrap_content"
                  android:layout_width="wrap_content" android:layout_weight="1"
                  android:background="@drawable/browse" android:textColor="#FFFFFF"
                  android:padding="10px"></Button>
      
              <Button android:id="@+id/search" android:layout_height="wrap_content"
                  android:layout_width="wrap_content" android:layout_weight="1"
                  android:background="@drawable/search" android:textColor="#FFFFFF"
                  android:padding="10px"></Button>
      
              <Button android:layout_height="wrap_content" android:id="@+id/favorite"
                  android:background="@drawable/favorite" android:layout_width="wrap_content"
                  android:layout_weight="1"
                  android:textColor="#FFFFFF" android:padding="10px"></Button>
          </LinearLayout>
      
      </LinearLayout>
      

      然后创建一个 HeaderFooter.java Activity

      public class HeaderFooter extends Activity {
              public void onCreate(Bundle savedInstanceState) {
                   super.onCreate(savedInstanceState);
                   setContentView(R.layout.headerfooter);
              }
       }
      

      现在将上述活动扩展到您的所有其他活动,并在 headerfooter.xml 的中间布局中扩展您的特定视图

      public class Home extends HeaderFooter 
      {
              @Override
              public void onCreate(Bundle savedInstanceState)
              {
                  super.onCreate(savedInstanceState);
                  ViewGroup vg = (ViewGroup) findViewById(R.id.lldata);
                  ViewGroup.inflate(Home.this, R.layout.home, vg);
              }
      }
      

      【讨论】:

      • 这种方法效果很好,谢谢! (唯一的问题是页脚和屏幕底部之间的间隙很小)
      • @CalebGomer 我已经更新了 xml 使用它,这个问题将得到解决..如果没有请告诉我
      • 解决了!再次感谢您的帮助。
      • @MKJParekh 您能否添加一个示例代码,说明如何在标题中收听按钮点击?我有一个问题,我无法扩展活动并膨胀标题没有显示任何内容,我没有收到任何错误,有什么建议吗?谢谢
      • 仅在 HeaderFooter 活动中,您可以编写按钮单击事件,该事件将自动扩展到其他类。在上面的代码中,我们在页眉页脚 xml 中膨胀数据部分。@Moe跨度>
      猜你喜欢
      • 1970-01-01
      • 2011-08-25
      • 2011-12-24
      • 1970-01-01
      • 2015-04-14
      • 1970-01-01
      • 2015-10-23
      • 2012-11-24
      相关资源
      最近更新 更多