android底部选项卡(一) TableHost
| 如上图:在开发中我们会经常遇到使用底部选项卡实现不同界面的切换,实现方法是多种多样: (1).TableHost (2).RadioGroup 、RadioButton (3).自定义布局 实现原理和RadioButton一致 (4).FragmentTabHost 下面附上代码:演示了TableHost的实现,但是应该注意,TableHost目前已过时,不推荐使用 |
1.定义布局文件
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity" >
- <!--TabHost布局定义:需要使用指定id="@android:id/tabhost"-->
- <TabHost
- android:id="@android:id/tabhost"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <TabWidget
- android:id="@android:id/tabs"
- android:layout_width="match_parent"
- android:layout_height="60px"
- android:layout_gravity="bottom" >
- </TabWidget>
- <FrameLayout
- android:id="@android:id/tabcontent"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_marginBottom="60px" >
- </FrameLayout>
- </TabHost>
- </RelativeLayout>
2.要在TabHost中显示的两个Activity的布局没有再次写出,其中的内容完全由自己设置
- package com.example.android_tablehost;
- import android.app.TabActivity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.Window;
- import android.widget.ImageView;
- import android.widget.TabHost;
- import android.widget.TabHost.TabSpec;
- import android.widget.TextView;
- public class MainActivity extends TabActivity {
- private TabHost mTabHost;
- private LayoutInflater inflater;
- @SuppressWarnings("deprecation")
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_main);
- inflater = LayoutInflater.from(this);
- // 直接调用TabActivity.getTabHost()方法获得xml文件中的TabHost对象
- mTabHost = getTabHost();
- // 添加底部标签
- mTabHost.addTab(getFirstTab());
- mTabHost.addTab(getSecondTab());
- mTabHost.setCurrentTabByTag("first");// 设置默认选中
- }
- /**
- * 内容1
- *
- * @return
- */
- private TabSpec getFirstTab() {
- // 添加AG
- TabSpec spec = mTabHost.newTabSpec("first");
- // 指定标签显示的内容 , 激活的activity对应的intent对象
- Intent intent = new Intent(this, FirsActivity.class);
- spec.setContent(intent);
- // 设置标签的文字和样式
- spec.setIndicator(getIndicatorView("标签一", R.drawable.tab_main_nav_me));
- return spec;
- }
- /**
- * 内容2
- *
- * @return
- */
- private TabSpec getSecondTab() {
- // 为标签指定tag
- TabSpec spec = mTabHost.newTabSpec("second");
- // 指定标签显示的内容 , 激活的activity对应的intent对象
- Intent intent = new Intent(this, SecondActivity.class);
- spec.setContent(intent);
- // 设置标签的文字和样式
- spec.setIndicator(getIndicatorView("标签2", R.drawable.tab_main_nav_book));
- return spec;
- }
- /**
- * 设置底部选项卡
- */
- private View getIndicatorView(String name, int iconid) {
- View view = inflater.inflate(R.layout.nav_tab, null);
- ImageView ivicon = (ImageView) view.findViewById(R.id.ivIcon);
- TextView tvtitle = (TextView) view.findViewById(R.id.tvTitle);
- ivicon.setImageResource(iconid);
- tvtitle.setText(name);
- return view;
- }
- }
浙公网安备 33010602011771号