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, 

以下是一个简单的样例:

首先我们要知道,什么是已经被加载的layout,什么是还没有加载的.我们启动一个应用,与入口Activity相关的layout{常见的是main.xml}就是被加载的,即在Oncreate()中的.而其它的layout是没有被加载的.就要动态加载了或通过还有一个activity.

在实际开发种LayoutInflater这个类还是很实用的,它的作用类似于 findViewById(),
不同点是LayoutInflater是用来找layout下xml布局文件,而且实例化!而findViewById()是找详细xml下的详细 widget控件.
为了让大家easy理解我[转]做了一个简单的Demo,主布局main.xml里有一个TextView和一个Button,当点击Button,出现 Dialog,而这个Dialog的布局方式是我们在layout文件夹下定义的custom_dialog.xml文件(里面左右分布,左边 ImageView,右边TextView)。
LayoutInflater作用及使用

代码例如以下:
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主要有两个重要方法,看代码:

  1. /**
  2. * A tab has a tab indicator, content, and a tag that is used to keep
  3. * track of it. This builder helps choose among these options.
  4. *
  5. * For the tab indicator, your choices are:
  6. * 1) set a label
  7. * 2) set a label and an icon
  8. *
  9. * For the tab content, your choices are:
  10. * 1) the id of a {@link View}
  11. * 2) a {@link TabContentFactory} that creates the {@link View} content.
  12. * 3) an {@link Intent} that launches an {@link android.app.Activity}.
  13. */
  14. public class TabSpec {
  15. private String mTag;
  16. private IndicatorStrategy mIndicatorStrategy;
  17. private ContentStrategy mContentStrategy;
  18. private TabSpec(String tag) {
  19. mTag = tag;
  20. }
  21. /**
  22. * Specify a label as the tab indicator.
  23. */
  24. public TabSpec setIndicator(CharSequence label) {
  25. mIndicatorStrategy = new LabelIndicatorStrategy(label);
  26. return this;
  27. }
  28. /**
  29. * Specify a label and icon as the tab indicator.
  30. */
  31. public TabSpec setIndicator(CharSequence label, Drawable icon) {
  32. mIndicatorStrategy = new LabelAndIconIndicatorStrategy(label, icon);
  33. return this;
  34. }
  35. /**
  36. * Specify a view as the tab indicator.
  37. */
  38. public TabSpec setIndicator(View view) {
  39. mIndicatorStrategy = new ViewIndicatorStrategy(view);
  40. return this;
  41. }
  42. /**
  43. * Specify the id of the view that should be used as the content
  44. * of the tab.
  45. */
  46. public TabSpec setContent(int viewId) {
  47. mContentStrategy = new ViewIdContentStrategy(viewId);
  48. return this;
  49. }
  50. /**
  51. * Specify a {@link android.widget.TabHost.TabContentFactory} to use to
  52. * create the content of the tab.
  53. */
  54. public TabSpec setContent(TabContentFactory contentFactory) {
  55. mContentStrategy = new FactoryContentStrategy(mTag, contentFactory);
  56. return this;
  57. }
  58. /**
  59. * Specify an intent to use to launch an activity as the tab content.
  60. */
  61. public TabSpec setContent(Intent intent) {
  62. mContentStrategy = new IntentContentStrategy(mTag, intent);
  63. return this;
  64. }


setIndicator()能够设置选项卡得图标和文字。

须要注意几点是: 1、假设你的Tabhost是从xml文件里findViewById()得到的,

TabWidget 必须为 android:id="@android:id/tabs" ,

FrameLayout android:id="@android:id/tabcontent" ;

  1. <TabHost android:id="@+id/tabhost_info" android:layout_width="fill_parent"
  2. android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
  3. <LinearLayout android:id="@+id/linearLayout"
  4. android:layout_width="fill_parent" android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <TabWidget android:id="@android:id/tabs"
  7. android:layout_width="fill_parent" android:layout_height="wrap_content">
  8. </TabWidget>
  9. <FrameLayout android:id="@android:id/tabcontent"
  10. android:layout_width="fill_parent" android:layout_height="wrap_content"
  11. android:layout_gravity="fill">
  12. <include android:id="@+id/info_include01" layout="@layout/info_layout01" />
  13. <include android:id="@+id/info_include02" layout="@layout/info_layout02" />
  14. <include android:id="@+id/info_include03" layout="@layout/info_layout03" />
  15. </FrameLayout>
  16. </LinearLayout>
  17. </TabHost>


2、代码中,在加入TabWidget前,须要调用setup()方法。

  1. tabHost=(TabHost)findViewById(R.id.tabhost_info);
  2. tabHost.setup();
  3. tabHost.addTab(tabHost.newTabSpec("信息")
  4. .setContent(R.id.info_include01)
  5. .setIndicator("基本信息",getResources().getDrawable(R.drawable.ic_launcher))
  6. );
  7. tabHost.addTab(tabHost.newTabSpec("很多其它信息")
  8. .setContent(R.id.info_include02)
  9. .setIndicator("很多其它信息",getResources().getDrawable(R.drawable.ic_launcher))
  10. tabHost.addTab(tabHost.newTabSpec("附件下载")
  11. .setContent(R.id.info_include03)
  12. .setIndicator("附件下载",getResources().getDrawable(R.drawable.ic_launcher))

以下是自己写的一个demo:

Demo下载地址 :http://download.csdn.net/detail/china1988s/4072957


原文地址:https://www.cnblogs.com/blfshiye/p/4082228.html