如何为Android应用程序创建页眉或页脚按钮栏

Cal*_*mer 12 xml android header footer android-layout

许多流行的应用程序,如谷歌地图,Facebook,Foursquare等,在他们的大多数活动中都有标题和/或页脚栏.这些标题通常包含非常有用的按钮,我想为我的应用程序创建一个.有谁知道他们是怎么做的?到目前为止我还没找到任何东西.

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

MKJ*_*ekh 14

使用这种方式,你可以制作header-footer xml并使用它,activity你只需要在HeaderFooter.java中为header-footer中的控件编写代码,并且可以访问它的项目.

构建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>
Run Code Online (Sandbox Code Playgroud)

然后创建一个HeaderFooter.java活动

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

现在将上面的活动扩展到所有其他活动,并在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);
        }
}
Run Code Online (Sandbox Code Playgroud)