【问题标题】:Navigation bar common to all activities所有活动通用的导航栏
【发布时间】:2012-10-01 03:19:26
【问题描述】:

我一直在寻找有关如何放置所有活动通用的导航栏的选项。仍然无法找出最好的方法来做到这一点。导航栏应该有一个屏幕标题和一个后退按钮。或者在某些活动中可能是两个。
我应该遵循的最佳做法是什么?

谢谢

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以像这样在单独的 header.xml 布局中定义导航栏:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/header_layout"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical">
    
      <!-- 1px border top -->
      <ImageView
        android:layout_width="fill_parent"
        android:layout_height="1px"
        android:background="@color/header_border_top" />
    
      <!-- header with text and buttons -->
      <RelativeLayout
        android:layout_height="43dp"
        android:layout_width="fill_parent"
        android:background="@color/header_background">
    
        <!-- left side -->
        <LinearLayout
          android:id="@+id/header_home_button_layout"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:orientation="horizontal"
          android:layout_alignParentLeft="true">
    
          <!-- home button -->
          <ImageButton
            android:id="@+id/header_home_button"
            android:src="@drawable/header_btn_home"
            android:onClick="onHomeClick" />
    
        </LinearLayout>
    
        <!-- header text -->
        <TextView
          android:id="@+id/header_title"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerInParent="true"
          android:text="@string/app_name"
          android:textSize="20dp"
          android:textStyle="bold"/>
    
        <!-- right side -->
        <LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:orientation="horizontal"
          android:layout_alignParentRight="true">
    
          <!-- back button -->
          <ImageButton
            android:id="@+id/header_back_button"
            android:src="@drawable/header_btn_back"
            android:onClick="onBackClick"
            android:visibility="gone" />
    
        </LinearLayout>
    
      </RelativeLayout>
    
      <!-- 1px border bottom -->
      <ImageView
        android:layout_width="fill_parent"
        android:layout_height="1px"
        android:background="@color/header_border_bottom" />
    
    </LinearLayout>
    

    然后在所有活动的布局中包含此标题:

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

    并确保您的所有类都扩展了具有 onHomeClick 和 onBackClick 方法的父类...

    【讨论】:

    • 感谢 peceps :) 在上面的答案中,有人建议我使用 ActionBar。你的建议也很方便。但是最好的方法是什么。请真诚的建议;)
    【解决方案2】:

    我的一个朋友创建了一个名为Android Actionbar 的开源项目,听起来你想做那个项目的工作。这是一个库项目,因此您可以在应用程序中使用它。网站上也有例子。

    这就是它的样子:

    【讨论】:

    • 还有ActionBarSherlock,在Android 1.x/2.x上使用上面的widget,在Android 3.x上使用原生Honeycomb操作栏:actionbarsherlock.com
    • 哇..这非常有用。非常感谢凯 :)
    • ActionBar 和 actionbar 示例都没有在 Eclipse 中导入。我怎样才能使它成为一个图书馆?
    • 看起来他们是 maven 项目。创建一个新的 android (eclipse) 项目,按照本指南:link,然后将文件复制到该项目。
    猜你喜欢
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多