效果图:
MainActivity.java
package tianshuai.homePage;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
public class MainActivity extends TabActivity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//取得当前的TabHost
TabHost tabs = getTabHost();
//载入主布局文件
LayoutInflater.from(this).inflate(R.layout.main, tabs.getTabContentView(), true);
//给Tab1添加自定义样式
RelativeLayout tabStyle1 = (RelativeLayout)LayoutInflater.from(this).inflate(R.layout.tab_style, null);
TextView text1 = (TextView)tabStyle1.findViewById(R.id.tab_label);
text1.setText("1");
//给Tab2添加自定义样式
RelativeLayout tabStyle2 = (RelativeLayout)LayoutInflater.from(this).inflate(R.layout.tab_style, null);
TextView text2 = (TextView)tabStyle2.findViewById(R.id.tab_label);
text2.setText("2");
//创建新Tab, 使用tab1的样式
TabSpec tab1 = tabs.newTabSpec("tab1");
tab1.setIndicator(tabStyle1);
tab1.setContent(R.id.Tab1);
tabs.addTab(tab1);
//创建新Tab, 使用tab2的样式
TabSpec tab2 = tabs.newTabSpec("tab2");
tab2.setIndicator(tabStyle2);
tab2.setContent(R.id.Tab2);
tabs.addTab(tab2);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id="@+id/Tab1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是tab1"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/Tab2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是tab2"
/>
</LinearLayout>
</FrameLayout>
table_style.xml
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="5dip"
android:paddingRight="5dip"
>
<TextView android:id="@+id/tab_label"
android:layout_width="fill_parent"
android:layout_height="30px"
android:gravity="center"
android:textColor="#000000"
android:textStyle="bold"
android:background="@drawable/tab"
/>
</RelativeLayout>
tab.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/red"
/>
<item
android:state_selected = "false"
android:drawable = "@drawable/bule"
/>
</selector>
分享到:
相关推荐
自定义tabhost,实现新浪微博效果
NULL 博文链接:https://jykenan.iteye.com/blog/855843
字定义TabHost,实现更换tabhost的背景颜色
android 自定义实现TAB, 简化开发代码,提供效率
使用了自定义button,自定义tabhost使用了不同默认的上标签切换页面
android的自定义TabHost,界面还是不错的哦,漂亮。
自定义tabhost实现activity 的跳转 界面比较简单 但功能都实现了
Android裡提供了TabHost,讓開發人員可以製做Tab版型;但是預設的樣式一點都不討喜。如果要自己重新設計版面其實也不太難,可直接运行,自定义的tabhost样式
很多情况下,我们需要用到标签,类似于微末下面的标签效果,直接使用系统默认的往往不能满足项目的需求,就需要我们自定义一个TabHost。下面我把在项目的实现的效果分享给大家,希望能帮助需要的朋友!
Android系统自带的TabHost控件很难看,这是一个自定义TabHost例子,显示在底部,类似于微博下面的Tab选项卡
Android Tabhost 实例demo 自定义Tabhost实现实例。
自定义Android中TabHost组件显示在屏幕底部,并实现滑动切换页面。在此基础上可以实现类似米聊等软件主界面的Tab效果。
Android实现tabHost控件自定义页签页面源码
android--解决方案--自定义tabhost(动态添加选项+带自动水平滑动选项卡+手势切换选项卡及内容功能)
今天开发Android短彩信传统模式,遇到了修改tab的默认样式问题,搜集了一些资料,可用参考。
实现自定义TabHost标签功能 底部demo
Android编程之Intent_TabHost源码演示,自定义的Tabhost(内含源码),学习制作Android软件的TAB标签界面,在电脑上已经很流行的功能,Android手机上当然不能少,这个TabHost源码相对简单些,是JAVA转学Android比较...
可自定义TabHost外观效果,源码Raised Center Tab,可以支持自定义TabHost外观,使中间的tab相对其他tab突起,也就是所谓的Raised Center Tab的了,大家可以了解一下先,喜欢的朋友可以收藏起来吧。 This ...
TabHost中填充自定义ListView
android自定义RadioButton,可用于竖直方向的tabhost组件的自定义设计,实现了tabWiget的效果。