TabHost的自定义
使用自定义的TabHost可以不用继承TabActicity,但是要注意的是如果使用Activity作为Content的话,有两处代码是一定要加的。不然就会出现RuntimeError,还有在XML布局中使用自定义的TabHost时除了TabHost的id自定义外,LinearLayout和TabWidget的id必须使用系统默认。具体可参考下面的Demo。
使用TabWidget的效果(左)和自定义View的效果图(右):
MainActivity加载的XML布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" > <TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0" /> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
</TabHost> </RelativeLayout>
MainActivity的Java代码(注意红色代码):
package com.hitech.tabhostdemo; import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec; @SuppressWarnings("deprecation")
public class MainActivity extends Activity { private LocalActivityManager lam;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); TabHost tabHost = (TabHost) findViewById(R.id.tabhost); // Intent对象
Intent intent1 = new Intent(this, TabSpecActivity1.class);
Intent intent2 = new Intent(this, TabSpecActivity2.class);
Intent intent3 = new Intent(this, TabSpecActivity3.class); // TabSpec对象
TabSpec tabSpec1 = tabHost.newTabSpec("first");
tabSpec1.setIndicator("第一页",
getResources().getDrawable(android.R.drawable.ic_dialog_email));
tabSpec1.setContent(intent1); TabSpec tabSpec2 = tabHost.newTabSpec("second");
tabSpec2.setIndicator("第二页",
getResources().getDrawable(android.R.drawable.ic_btn_speak_now));
tabSpec2.setContent(intent2); TabSpec tabSpec3 = tabHost.newTabSpec("third");
tabSpec3.setIndicator("第三页",
getResources().getDrawable(android.R.drawable.ic_dialog_dialer));
tabSpec3.setContent(intent3); lam = new LocalActivityManager(this, false);
lam.dispatchCreate(savedInstanceState);
tabHost.setup(lam);
tabHost.addTab(tabSpec1);
tabHost.addTab(tabSpec2);
tabHost.addTab(tabSpec3); // TabActivity
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
} @Override
protected void onPause() {
super.onPause();
lam.dispatchPause(isFinishing());
}
}
自定义View代替TabWidget效果的实现原理:隐藏TabWidget,然后在线性布局中加入自定义View,如上图中即为LinearLayout中添加三个TextView,再分别对TextView设置点击事件来模拟TabWidget的效果。
同样,MainActivity加载的XML布局如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" > <TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal"android:visibility="gone" /> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" /> <View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="1dp"
android:background="@android:color/background_light" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <TextView
android:id="@+id/tab_convst"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawablePadding="-10dp"
android:drawableTop="@drawable/ic_widget_conversation"
android:gravity="center"
android:paddingBottom="5dp"
android:text="@string/conversation" /> <TextView
android:id="@+id/tab_folder"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawablePadding="-10dp"
android:drawableTop="@drawable/ic_widget_folder"
android:gravity="center"
android:paddingBottom="5dp"
android:text="@string/folder" /> <TextView
android:id="@+id/tab_group"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawablePadding="-10dp"
android:drawableTop="@drawable/ic_widget_group"
android:gravity="center"
android:paddingBottom="5dp"
android:text="@string/group" />
</LinearLayout>
</LinearLayout>
</TabHost> </RelativeLayout>
注意和以往不同之处已做标记,分别是TabWidget和自定义的LinearLayout。
MainActivity中的处理:
package com.hitech.capacitymessage; import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView; @SuppressWarnings("deprecation")
public class MainActivity extends Activity implements OnClickListener { private static final String TAG = "MainActivity";
private LocalActivityManager lam;
private TabHost tabHost;
private Bundle state;
private TabSpec tab1;
private TabSpec tab2;
private TabSpec tab3; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.state = savedInstanceState;
activityInitializer();
} private void activityInitializer() {
tabHost = (TabHost) findViewById(R.id.tabhost);
tab1 = tabHost.newTabSpec("tab1");
tab1.setIndicator("会话",
getResources().getDrawable(R.drawable.ic_widget_conversation));
tab1.setContent(new Intent(this, TabActivity1.class)); tab2 = tabHost.newTabSpec("tab2");
tab2.setIndicator("文件夹",
getResources().getDrawable(R.drawable.ic_widget_folder));
tab2.setContent(new Intent(this, TabActivity2.class)); tab3 = tabHost.newTabSpec("tab3");
tab3.setIndicator("群组",
getResources().getDrawable(R.drawable.ic_widget_group));
tab3.setContent(new Intent(this, TabActivity3.class)); lam = new LocalActivityManager(this, true);
lam.dispatchCreate(state);
tabHost.setup(lam); tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3); TextView block1 = (TextView) findViewById(R.id.tab_convst);
TextView block2 = (TextView) findViewById(R.id.tab_folder);
TextView block3 = (TextView) findViewById(R.id.tab_group); block1.setOnClickListener(this);
block2.setOnClickListener(this);
block3.setOnClickListener(this);
} @Override
public void onClick(View v) {
String tabTag = tabHost.getCurrentTabTag();
switch (v.getId()) {
case R.id.tab_convst:
if (!tabTag.equals("tab1")) {
tabHost.setCurrentTabByTag("tab1");
}
Log.e(TAG, tabHost.getCurrentTabTag());
break;
case R.id.tab_folder:
if (!tabTag.equals("tab2")) {
tabHost.setCurrentTabByTag("tab2");
}
Log.e(TAG, tabHost.getCurrentTabTag());
break;
case R.id.tab_group:
if (!tabTag.equals("tab3")) {
tabHost.setCurrentTabByTag("tab3");
}
Log.e(TAG, tabHost.getCurrentTabTag());
break;
default:
break;
}
} @Override
protected void onPause() {
super.onPause();
lam.dispatchPause(isFinishing());
}
}
TabHost的自定义的更多相关文章
- 【读书笔记《Android游戏编程之从零开始》】6.Android 游戏开发常用的系统控件(TabHost、ListView)
3.9 TabSpec与TabHost TabHost类官方文档地址:http://developer.android.com/reference/android/widget/TabHost.htm ...
- TabHost自定义外观
博客园:http://www.cnblogs.com 农民伯伯: http://www.cnblogs.com/over140 版本 新浪微博 weibo_10235010.apk 正文 一.效果图 ...
- 使用自定义RadioButton和ViewPager实现TabHost效果和带滑动的页卡效果
在工作中又很多需求都不是android系统自带的控件可以达到效果的,内置的TabHost就是,只能达到简单的效果 ,所以这个时候就要自定义控件来达到效果:这个效果就是: 使用自定义RadioButto ...
- android自定义tabhost,tabcontent用intent获得
地址:http://my.oschina.net/aowu/blog/36282 自己改的自定义tabhost组建,效果图如左.有更好的朋友可以相互交流一下,嘿嘿. 1.先上AndroidManife ...
- 自定义TabHost,TabWidget样式
先看效果: 京东商城底部菜单栏 新浪微博底部菜单栏 本次学习效果图:
- 安卓自定义类似TabHost的导航栏
有时候为了项目需要我们要自定义一些导航控件,类似下面这样. 下面给大家讲讲我是怎么实现的, 1.素材准备(这个都是美工的事情) 2.①资源文件共有五个 如下: activity_main_first. ...
- Android 自定义TabHost,TabWidget样式
界面比较简单,要想做得漂亮换几张图片就可以了. 第一步:先在布局(这里用了main.xml创建时自动生成的)里面放上TabHost ,只要将TabHost控件托至屏幕中就可: <?xml ver ...
- Android 实现分页(使用TabWidget/TabHost)
注:本文为转载,但该内容本人已亲身尝试,确认该方法可行,代码有点小的改动,转载用作保存与分享. 原作者地址:http://gundumw100.iteye.com/blog/853967 个人吐嘈:据 ...
- 利用TabHost制作QQ客户端标签栏效果(低版本QQ)
学习一定要从基础学起,只有有一个好的基础,我们才会变得更加的perfect 下面小编将利用TabHost制作QQ客户端标签栏效果(这个版本的QQ是在前几年发布的)…. 首先我们看一下效果: 看到这个界 ...
随机推荐
- Hibernate中的HQL
一.查询所有的时候 List<Company> list=session.createQuery("from Company as c order by c.cid desc&q ...
- ZooKeeper伪集群环境搭建
1.从官网下载程序包. 2.解压. [dev@localhost software]$ tar xzvf zookeeper-3.4.6.tar.gz 3.进入zookeeper文件夹后创建data文 ...
- 认识一下Kotlin语言,Android平台的Swift
今天在CSDN首页偶然看到一个贴子JetBrains正式公布Kotlin 1.0:JVM和Android上更好用的语言 看完后,感觉Kotlin语法非常简洁,有一系列动态语言的特点,Lambda表达式 ...
- poj3071之概率DP
Football Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2667 Accepted: 1361 Descript ...
- Gradle 编译多个project(包括多Library库project依赖)指导
Gradle Android最新自己主动化编译脚本教程(提供demo源代码) 这篇文章我简单写了基于Gradle2.1 进行的android project和android library的编译实例, ...
- linux使用windows中编辑的文件,格式问题
参考:https://blog.csdn.net/yongan1006/article/details/8142527 运行脚本时出现了这样一个错误,打开之后并没有找到所谓的^M,查了之后才知道原来是 ...
- 安卓 使用Gradle生成正式签名apk文件
1. 进入app中的build.gradle下面进行配置 2.进入Gradle下面选择clean和assembleRelese,双击 3.生成成功,前往查看 4.加密更安全
- JS基本功 | JavaScript专题之数组 - 方法总结
Array.map() 1. map() 遍历数组 语法: let new_array = arr.map(function callback(currentValue, index, array ...
- HTML基础——网站后台显示页面
1.框架集标签:(作用:将页面进行区域的划分) <frameset rows="" cols=""> <frame src="&qu ...
- Ubuntu 14.04下安装CUDA8.0
配置环境如下: 系统:Ubuntu14.04 64bit 显卡:Nvidia K620M 显卡驱动:Nvidia-Linux-x86_64-375.66.run CUDA8.0 + cudnn8.0 ...