自定义 Tabhost 样式
来源:互联网 发布:mysql insert很慢 编辑:程序博客网 时间:2024/05/21 18:46
经过了一天的折腾, 在网上也找了不少源码, 但是效果都不是很好,非常长, 最后自己终于写出了一个代码不是很多的Tabhost。
先上图(背景是随便弄的, 所以不怎么样)
首先, 我们要先写一个主xml main.xml
每一个LinearLayout代表一个Tab内容
<?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>
接下来是 tab样式的xml tab_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" android:background = "@drawable/topbar_background" > <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_bg" /> </RelativeLayout>
接下来就是tab 按下的状态 一个selector
这里topbar_bg_down是按下的图片
<?xml version="1.0" encoding="UTF-8"?><selector xmlns:android = "http://schemas.android.com/apk/res/android"> <item android:state_focused = "true" android:drawable = "@drawable/topbar_bg_down" /> <item android:state_selected = "true" android:drawable = "@drawable/topbar_bg_down" /> <item android:state_pressed = "true" android:drawable = "@drawable/topbar_bg_down" /> </selector>
最后是主 Activity的代码
public class MissedHelperActivity extends TabActivity{/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);//取得当前的TabHostTabHost 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);}}
到这里就结束了, 代码里调用了几个图片, 这里就不放出来了。
- 自定义TabHost,TabWidget样式
- 自定义 Tabhost 样式
- 自定义TabHost,TabWidget样式 .
- 自定义TabHost,TabWidget样式 .
- 自定义TabHost,TabWidget样式 .
- 自定义TabHost,TabWidget样式 .
- 自定义TabHost,TabWidget样式
- 自定义TabHost,TabWidget样式 .
- android Tabhost部件(三)--使用自定义view和fragement实现类似TabHost样式
- 自定义TabHost
- 自定义TabHost
- 自定义TabHost
- 自定义TabHost
- 自定义TabHost
- 修改TabHost默认样式
- 修改TabHost默认样式
- android 自定义tabhost
- Android 自定义tabhost
- linux驱动模型开发2——linux platfoem总线机制讲解与实例开发
- C# 结束任务栏指定进程
- Android 实现书籍翻页效果----完结篇
- android 浏览器问题
- 用于RTC的32.768kHz晶振电路的设计
- 自定义 Tabhost 样式
- 不为人知的 javascript,你懂多少呢?
- 一步一步写算法(之图添加和删除)
- POJ FOUR QUARTERS
- 关于mis系统自动构建的一些思考
- 【出错】ubuntu 11.10库链接问题
- 遍历dom树下的素有元素处理例子
- 浮躁浮躁,感觉自己最近太浮躁了!
- <jsp:include>与include指令的区别