首页 > 代码库 > TabHost 简单使用方法
TabHost 简单使用方法
package com.google.tabhost;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
public class HelloTabHost extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//调用TabActivity的getTabHost()方法获取TabHost对象
TabHost tabHost = getTabHost();
//设置使用TabHost布局
LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(),true);
//添加第一个标签页
tabHost.addTab(tabHost.newTabSpec("tab01").setIndicator("已接电话").setContent(R.id.tab01));
//添加第二个标签页,并在其标签上添加一个图片
tabHost.addTab(tabHost.newTabSpec("tab02").setIndicator("未接电话",getResources().getDrawable(R.drawable.icon)).setContent(R.id.tab02));
//添加第三个标签页
tabHost.addTab(tabHost.newTabSpec("tab03").setIndicator("已拨电话").setContent(R.id.tab03));
}
}
LayoutInflater作用及使用
作用:
1、对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入.
2、对于一个已经载入的Activity, 就可以使用实现了这个Activiyt的的findViewById方法来获得其中的界面元素.
方法:
Android里面想要创建一个画面的时候, 初学一般都是新建一个类, 继承Activity基类, 然后在onCreate里面使用setContentView方法来载入一个在xml里定义好的界面.
其实在Activity里面就使用了LayoutInflater来载入界面, 通过getSystemService(Context.LAYOUT_INFLATER_SERVICE)方法可以获得一个 LayoutInflater, 也可以通过LayoutInflater inflater = getLayoutInflater();来获得.然后使用inflate方法来载入layout的xml,
1、对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入.
2、对于一个已经载入的Activity, 就可以使用实现了这个Activiyt的的findViewById方法来获得其中的界面元素.
方法:
下面是一个简单的例子:
首先我们要知道,什么是已经被载入的layout,什么是还没有载入的.我们启动一个应用,与入口Activity相关的layout{常见的是main.xml}就是被载入的,即在Oncreate()中的.而其他的layout是没有被载入的.就要动态载入了或通过另一个activity.
在实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于 findViewById(),
不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件.
为了让大家容易理解我[转]做了一个简单的Demo,主布局main.xml里有一个TextView和一个Button,当点击Button,出现 Dialog,而这个Dialog的布局方式是我们在layout目录下定义的custom_dialog.xml文件(里面左右分布,左边 ImageView,右边TextView)。
代码如下:
package com.bivin;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
showCustomDialog();
}
public void showCustomDialog() {
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = MainActivity.this;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog, null);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, Welcome to Mr Wei‘s blog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
}
}
TabSpec与TabHost
TabHost相当于浏览器中浏览器分布的集合,而Tabspec则相当于浏览器中的每一个分页面。d在Android中,每一个TabSpec分布可以是一个组件,也可以是一个布局,然后将每一个分页装入TabHost中,TabHost即可将其中的每一个分页一并显示出来
Android 中的Tabhost控件是个挺好用的控件,像一些分模块展示的页面就可以用Tabhost。
Tabhost的主要是由TabSpac组成的选项卡集合。TabSpec主要有两个重要方法,看代码:
[java] view plaincopyprint?
- /**
- * A tab has a tab indicator, content, and a tag that is used to keep
- * track of it. This builder helps choose among these options.
- *
- * For the tab indicator, your choices are:
- * 1) set a label
- * 2) set a label and an icon
- *
- * For the tab content, your choices are:
- * 1) the id of a {@link View}
- * 2) a {@link TabContentFactory} that creates the {@link View} content.
- * 3) an {@link Intent} that launches an {@link android.app.Activity}.
- */
- public class TabSpec {
- private String mTag;
- private IndicatorStrategy mIndicatorStrategy;
- private ContentStrategy mContentStrategy;
- private TabSpec(String tag) {
- mTag = tag;
- }
- /**
- * Specify a label as the tab indicator.
- */
- public TabSpec setIndicator(CharSequence label) {
- mIndicatorStrategy = new LabelIndicatorStrategy(label);
- return this;
- }
- /**
- * Specify a label and icon as the tab indicator.
- */
- public TabSpec setIndicator(CharSequence label, Drawable icon) {
- mIndicatorStrategy = new LabelAndIconIndicatorStrategy(label, icon);
- return this;
- }
- /**
- * Specify a view as the tab indicator.
- */
- public TabSpec setIndicator(View view) {
- mIndicatorStrategy = new ViewIndicatorStrategy(view);
- return this;
- }
- /**
- * Specify the id of the view that should be used as the content
- * of the tab.
- */
- public TabSpec setContent(int viewId) {
- mContentStrategy = new ViewIdContentStrategy(viewId);
- return this;
- }
- /**
- * Specify a {@link android.widget.TabHost.TabContentFactory} to use to
- * create the content of the tab.
- */
- public TabSpec setContent(TabContentFactory contentFactory) {
- mContentStrategy = new FactoryContentStrategy(mTag, contentFactory);
- return this;
- }
- /**
- * Specify an intent to use to launch an activity as the tab content.
- */
- public TabSpec setContent(Intent intent) {
- mContentStrategy = new IntentContentStrategy(mTag, intent);
- return this;
- }
/** * A tab has a tab indicator, content, and a tag that is used to keep * track of it. This builder helps choose among these options. * * For the tab indicator, your choices are: * 1) set a label * 2) set a label and an icon * * For the tab content, your choices are: * 1) the id of a {@link View} * 2) a {@link TabContentFactory} that creates the {@link View} content. * 3) an {@link Intent} that launches an {@link android.app.Activity}. */ public class TabSpec { private String mTag; private IndicatorStrategy mIndicatorStrategy; private ContentStrategy mContentStrategy; private TabSpec(String tag) { mTag = tag; } /** * Specify a label as the tab indicator. */ public TabSpec setIndicator(CharSequence label) { mIndicatorStrategy = new LabelIndicatorStrategy(label); return this; } /** * Specify a label and icon as the tab indicator. */ public TabSpec setIndicator(CharSequence label, Drawable icon) { mIndicatorStrategy = new LabelAndIconIndicatorStrategy(label, icon); return this; } /** * Specify a view as the tab indicator. */ public TabSpec setIndicator(View view) { mIndicatorStrategy = new ViewIndicatorStrategy(view); return this; } /** * Specify the id of the view that should be used as the content * of the tab. */ public TabSpec setContent(int viewId) { mContentStrategy = new ViewIdContentStrategy(viewId); return this; } /** * Specify a {@link android.widget.TabHost.TabContentFactory} to use to * create the content of the tab. */ public TabSpec setContent(TabContentFactory contentFactory) { mContentStrategy = new FactoryContentStrategy(mTag, contentFactory); return this; } /** * Specify an intent to use to launch an activity as the tab content. */ public TabSpec setContent(Intent intent) { mContentStrategy = new IntentContentStrategy(mTag, intent); return this; }
setIndicator()可以设置选项卡得图标和文字。
需要注意几点是: 1、如果你的Tabhost是从xml文件中findViewById()得到的,
TabWidget 必须为 android:id="@android:id/tabs" ,
FrameLayout android:id="@android:id/tabcontent" ;
[html] view plaincopyprint?
- <TabHost android:id="@+id/tabhost_info" android:layout_width="fill_parent"
- android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android">
- <LinearLayout android:id="@+id/linearLayout"
- android:layout_width="fill_parent"android:layout_height="fill_parent"
- android:orientation="vertical">
- <TabWidgetandroid: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="wrap_content"
- android:layout_gravity="fill">
- <include android:id="@+id/info_include01" layout="@layout/info_layout01" />
- <include android:id="@+id/info_include02" layout="@layout/info_layout02" />
- <include android:id="@+id/info_include03" layout="@layout/info_layout03" />
- </FrameLayout>
- </LinearLayout>
- </TabHost>
<TabHost android:id="@+id/tabhost_info" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayout android:id="@+id/linearLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <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="wrap_content" android:layout_gravity="fill"> <include android:id="@+id/info_include01" layout="@layout/info_layout01" /> <include android:id="@+id/info_include02" layout="@layout/info_layout02" /> <include android:id="@+id/info_include03" layout="@layout/info_layout03" /> </FrameLayout> </LinearLayout> </TabHost>
2、代码中,在添加TabWidget前,需要调用setup()方法。
[java] view plaincopyprint?
- tabHost=(TabHost)findViewById(R.id.tabhost_info);
- tabHost.setup();
- tabHost.addTab(tabHost.newTabSpec("信息")
- .setContent(R.id.info_include01)
- .setIndicator("基本信息",getResources().getDrawable(R.drawable.ic_launcher))
- );
- tabHost.addTab(tabHost.newTabSpec("更多信息")
- .setContent(R.id.info_include02)
- .setIndicator("更多信息",getResources().getDrawable(R.drawable.ic_launcher))
- tabHost.addTab(tabHost.newTabSpec("附件下载")
- .setContent(R.id.info_include03)
- .setIndicator("附件下载",getResources().getDrawable(R.drawable.ic_launcher))
tabHost=(TabHost)findViewById(R.id.tabhost_info); tabHost.setup(); tabHost.addTab(tabHost.newTabSpec("信息") .setContent(R.id.info_include01) .setIndicator("基本信息",getResources().getDrawable(R.drawable.ic_launcher)) ); tabHost.addTab(tabHost.newTabSpec("更多信息") .setContent(R.id.info_include02) .setIndicator("更多信息",getResources().getDrawable(R.drawable.ic_launcher)) ); tabHost.addTab(tabHost.newTabSpec("附件下载") .setContent(R.id.info_include03) .setIndicator("附件下载",getResources().getDrawable(R.drawable.ic_launcher)) );
下面是自己写的一个demo:
Demo下载地址 :http://download.csdn.net/detail/china1988s/4072957
。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。