打开Android手机中的呼叫按键,里面显示出的就是导航控件,我们可以单击一个,然后让下面显示不同或者相同布局的页面。两种实现方式
一:继承自TabActivitty
继承的导航,导航栏中只能是文字,且导航栏样式比较的单一
具体实现:
◆主Activity
|
◆单击导航时页面跳转的三个Activity只需分别制指定三个xml文件即可,这三个xml文件只是添加了一个按钮。
◆结果
二:自定义导航
1:创建
(1)创建xml文件,创建TabHost节点,ID自行设定。
(2)创建TabWidget子节点,用于设定标签,ID必须为"@android:id/tabs"。每一个标签会有一个xml文件中定义的样式决定。比如这个导航标签中可以为上面是一幅图片,下面是一个文字,那么这样的样式可以在xml文件中定义。
(3)创建FrameLayout子节点,用于显示内容,Id必须为"@android:id/tabcontent"。有几个标签,FrameLayout就有几个布局视图。比如有单个导航标签,则就有三个RelativeLayout(这里只是举个例子,也可以为其他布局),在这个布局中可以设定自己的样式。
2:具体实现
(1)TabHost的selftabhost.xml文件
<?xml version="1.0" encoding="utf-8"?> <!-- 注意这里没有布局,直接就是一个TabHost --> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center" > <!-- 导航标签 --> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" > </TabWidget> <!-- 用于显示内容的部分 --> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" > <!-- 里面有几个RelativeLayout就有几个界面要显示 --> <RelativeLayout android:id="@+id/daohang1" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <!-- 定义界面中的样式,这里为一个按钮 --> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="导航1对应界面上的按钮1" /> </RelativeLayout> <RelativeLayout android:id="@+id/daohang2" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="导航2对应界面上的按钮2" /> </RelativeLayout> <RelativeLayout android:id="@+id/daohang3" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="导航3对应界面上的按钮3" /> </RelativeLayout> </FrameLayout> </LinearLayout> </TabHost> |
(2)导航标签的样式xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" > <ImageView android:id="@+id/p_w_picpathView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout> |
(3)主Activity:SelfTabHost
|
(4)结果
转载于:https://blog.51cto.com/cinderella7/1282212