地址:http://my.oschina.net/aowu/blog/36282

自己改的自定义tabhost组建,效果图如左。有更好的朋友可以相互交流一下,嘿嘿。

1.先上AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.priscilla"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-sdk android:minSdkVersion="7" />

<application
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".MyTab" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".TabContent" >
        </activity>
    </application>

</manifest>

2  MyTab.java

package com.priscilla;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
//import android.view.LayoutInflater;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;

public class MyTab extends TabActivity {
 /** Called when the activity is first created. */

private TabHost mTabhost;
 private TabWidget mTabWidget;
 private LayoutInflater mInflater;
 private List<TextView> mtext;
 private List<TabSpec> mTabSpec;
 private List<LinearLayout> linearLayout;
 private List<Intent> intent; 
 private Context mContext;

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

mContext = this;
  mInflater = LayoutInflater.from(this);

// mTabhost = this.getTabHost();// 从TabActivity上面获取放置Tab的TabHost
  mTabhost = (TabHost) findViewById(android.R.id.tabhost);
  mTabWidget = (TabWidget) findViewById(android.R.id.tabs);

mTabSpec = new ArrayList<TabSpec>();
  linearLayout = new ArrayList<LinearLayout>();
  mtext = new ArrayList<TextView>();
  intent = new ArrayList<Intent>();

creatTab();

/*
   * mTabhost.addTab(mTabhost .newTabSpec("选项卡1") .setIndicator(
   * (LinearLayout) LayoutInflater.from(this).inflate( R.layout.tabwidget,
   * null)) .setContent(R.id.linearLayout01)); mTabhost.addTab(mTabhost
   * .newTabSpec("选项卡2") .setIndicator( (LinearLayout)
   * LayoutInflater.from(this).inflate( R.layout.tabwidget, null))
   * .setContent(R.id.linearLayout02)); mTabhost.addTab(mTabhost
   * .newTabSpec("选项卡3") .setIndicator( (LinearLayout)
   * LayoutInflater.from(this).inflate( R.layout.tabwidget, null))
   * .setContent(R.id.linearLayout03));
   */

// 设置当前显示哪一个标签
  // mTabhost.setCurrentTab(0);

// 去tabwidget白线,这个对默认主题有效
  /*
   * mTabhost.setPadding(mTabhost.getPaddingLeft(),
   * mTabhost.getPaddingTop(), mTabhost.getPaddingRight(),
   * mTabhost.getPaddingBottom() - 5);
   */
 }

public void creatTab() {

for (int i = 0; i < 3; i++) {
   mTabSpec.add(mTabhost.newTabSpec("tab" + i));
   linearLayout.add((LinearLayout) mInflater.inflate(
     R.layout.tabwidget, null));
   mtext.add((TextView) linearLayout.get(i)
     .findViewById(R.id.tab_name));
   mtext.get(i).setText(
     mContext.getResources().getString(R.string.tab_name,
       String.valueOf(i)));
   mTabSpec.get(i).setIndicator(linearLayout.get(i));
   //mTabSpec.get(i).setContent(list.get(i));
   intent.add(new Intent(mContext, TabContent.class));
   Log.v("---whty---", mTabSpec.get(i).getTag());
   Bundle buddle = new Bundle();
   buddle.putString("tab", mTabSpec.get(i).getTag());
   intent.get(i).putExtras(buddle);
   
   mTabSpec.get(i).setContent(intent.get(i));

mTabhost.addTab(mTabSpec.get(i));

}
 }

}

2.TabContent.java

package com.priscilla;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class TabContent extends Activity {

private TextView textView;
 private ImageView imgView;

@Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.tabcontent);

textView = (TextView) findViewById(R.id.TextView);
  imgView = (ImageView) findViewById(R.id.ImageView);

Bundle buddle = this.getIntent().getExtras();
  String flag = buddle.getString("tab");

if ("tab0".equals(flag)) {
   textView.setText(getResources().getString(R.string.andy));
   Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
     R.drawable.andy);
   imgView.setImageBitmap(bitmap);
  }

if ("tab1".equals(flag)) {
   textView.setText(getResources().getString(R.string.bill));
   Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
     R.drawable.bill);
   imgView.setImageBitmap(bitmap);
  }

if ("tab2".equals(flag)) {
   textView.setText(getResources().getString(R.string.linux));
   Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
     R.drawable.torvalds);
   imgView.setImageBitmap(bitmap);
  }

}

}

3.3个layout

main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/wcity_normal_bg" >

<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"
            android:background="@drawable/con_film_bottom_tab" />

<FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </FrameLayout>
    </LinearLayout>

</TabHost>

tabwidget.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" 
 android:orientation="horizontal"
 android:gravity="center" >
 <TextView 
  android:id="@+id/tab_name" 
  android:layout_width="fill_parent" 
  android:layout_height="39dp"
  android:layout_marginLeft="2dp"
  android:layout_marginRight="2dp"
  android:ellipsize="marquee"  
  android:marqueeRepeatLimit="marquee_forever"
  android:singleLine="true"
  android:gravity="center" 
  android:textColor="@drawable/tab_selector"
  android:background="@drawable/tab_bg_selector" />
</LinearLayout>

tabcontent.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

<ImageView
        android:id="@+id/ImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:scaleType="fitXY" />

<TextView
        android:id="@+id/TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24dip" />

</LinearLayout>

4.2个drawable

tab_bg_selector.xml

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_selected="true" android:drawable="@drawable/con_film_tab_pressed"  />
</selector>

tab_selector.xml

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_selected="false" android:color="#ffffffff"/>
 <item android:state_selected="true" android:color="#ffEEC900"/>
</selector>

6.colors.xml  string.xml

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="wcity_normal_bg">#ffe5eff4</color>
</resources>

string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, my</string>
    <string name="app_name">myTab</string>
    <string name="andy">Android的创造者: Andy Rubin</string>
    <string name="bill">Java创造者之一: Bill Joy</string>
    <string name="linux">Linux之父: Linus Torvalds</string>
    <string name="tab_name">选项卡%1$s</string>    
</resources>

android自定义tabhost,tabcontent用intent获得的更多相关文章

  1. Android 自定义TabHost,TabWidget样式

    界面比较简单,要想做得漂亮换几张图片就可以了. 第一步:先在布局(这里用了main.xml创建时自动生成的)里面放上TabHost ,只要将TabHost控件托至屏幕中就可: <?xml ver ...

  2. Android项目--tabhost

    所有牵扯到自定义布局的layout中尽量用RelativeLayout 在通讯录中如果像小米手机的UI那就是viewpager,在这里,我们做成静态的.通过tabhost来做. 1.布局 a) 直接用 ...

  3. Android之TabHost实现Tab切换

    TabHost是整个Tab的容器,包含TabWidget和FrameLayout两个部分,TabWidget是每个Tab的表情,FrameLayout是Tab内容. 实现方式有两种: 1.继承TabA ...

  4. android中TabHost和RadioGroup

    android底部菜单应用 博客分类: android--UI示例 TabHostMenuRadioGroupButton  在android中实现菜单功能有多种方法. Options Menu:用户 ...

  5. Android底部TabHost API

    今天在项目中遇到了底部TabHost,顺便就写了一个底部TabHost的api继承即可使用非常简单,以下为源代码: 首先是自定义的TabHostActivity,如果要使用该TabHost继承该类即可 ...

  6. android自定义TabWidget样式

    先看看效果图吧,个人觉得图标丑了点,不过还行,自己用PS做的 下面是全部代码和流程,一定要按流程顺序来,不然错误! 1.tabhost.xml <TabHost xmlns:android=&q ...

  7. Android:TabHost实现Tab切换

    TabHost是整个Tab的容器,包含TabWidget和FrameLayout两个部分,TabWidget是每个Tab的表情,FrameLayout是Tab内容. 实现方式有两种: 1.继承TabA ...

  8. Android选项卡TabHost方式实现

    1.布局XML: <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android= ...

  9. android使用tabhost实现导航

    参考 http://blog.csdn.net/xixinyan/article/details/6771341 http://blog.sina.com.cn/s/blog_6b04c8eb0101 ...

随机推荐

  1. HDU 4091 Zombie’s Treasure Chest 分析 难度:1

    Zombie’s Treasure Chest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  2. Flask初级(一)创建及运行flash

    和前面的django差不多,选个类型,定义个目录. 选个模板解释器,定义一个模板文件夹名称. Create就创建成功了. 运行一下.会显示 Hello World! 最好给它换个运行环境,建个虚拟环境 ...

  3. Nodejs之mssql模块的封装

    在nodejs中,mssql模块支持sqlserver数据库操作.今天将mssql模块的某些功能封装为一个类,方便以后调用.封装的功能有执行存储过程,执行查询语句操作等.如果本篇文章对大家有帮助,那就 ...

  4. Goolge全球各国域名大全

    搜索过技术文章的朋友都知道,Google的搜索功力绝对是世界第一,仅仅搜索中文还体现不出Google的功力,如果大家是做外贸或者搜索其它语言的文章,就会知道Google的内涵有多少了.全世界的国家Go ...

  5. android机顶盒真机调试方法

    最近接触电视APP开发,之前对Android开发也不太了解还一直以为不能真机调试.最近静下心来想一想肯定能真机调试的,我是我不知道而已.现在讲述一下真机调试的步骤: 1.进入设置--关于,连续点击版本 ...

  6. clipboard.js 实现web端---> 复制到剪切板功能

    package.json 中: 添加clipboard 依赖 "dependencies": { "vue": "^2.2.2", &quo ...

  7. 强化学习 CartPole实验的一些启发 有没有可能设计一个新的实验呢?(杆子可以向360度方向倾倒,可行吗?)

    最近在看强化学习方面的东西,突然想到了这么一个事情,那就是经典的CartPole游戏我们改变一下,或者说升级一下,那么使用强化学习是否能得到不错的效果呢? 原始游戏如图: 一点个人的想法: ===== ...

  8. 【机器学习算法】AdaBoost自适应提升算法

    前言 AdaBoost的算法步骤比较容易理解,可以参考李航老师的<统计学习方法>和July的blog. 对博主而言,最主要的是迭代部分的第二步骤是如何如何确定阈值呢,也就是说有一个特征就有 ...

  9. 20155220 2016-2017-2 《Java程序设计》第九周学习总结

    20155220 2016-2017-2<Java程序设计>第九周学习总结 教材学习内容总结 JDBC(Java DataBase Connectivity)即java数据库连接,是一种用 ...

  10. Executor 框架详解

    框架最核心的类是ThreadPoolExecutor,它是Java线程池的实现类,通过Executors工具类,可以创建3种类型的ThreadPoolExecutor: 首先附上ThreadPoolE ...