android--解--它们的定义tabhost(动态添加的选项+用自己的主动性横向滑动标签+手势切换标签页和内容特征)
在本文中,解决他们自己的定义tabhost实现,并通过代码集成动态加入标签功能、自己主动标签横向滑动功能、和手势标签按功能之间切换。
我完成了这个完美的解决方案一起以下:
1、定义tabwidget布局选项卡:tab_button.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="119dip"
android:layout_height="wrap_content"
android:orientation="vertical" > <TextView
android:id="@+id/tv_tabs_tabHost"
android:layout_width="119dip"
android:layout_height="39dip"
android:gravity="center"
android:text="tab1" /> </LinearLayout>
2、主页面tab控件的布局文件:activity_tabhost.xml
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <!-- 水平滚动 --> <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" > <HorizontalScrollView
android:id="@+id/hScroller_mytabhostactivity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fadingEdge="none"
android:saveEnabled="false"
android:scrollbars="none" > <TabWidget
android:id="@android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</HorizontalScrollView>
</RelativeLayout> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
</TabHost>
3、主页面MyTabHostActivity.java
首先定义例如以下变量:
// tabhost
private static TabHost tbProductHost;
// 滑动手势
private GestureDetector detector;
// tab widget水平滑动条
private HorizontalScrollView hScroller;
private int screenWidth;// 屏幕宽度 单位:dp
当中oncreate方法:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabhost);
// 初始化tabhost
tbProductHost = (TabHost) findViewById(R.id.tabhost);
tbProductHost.setup(CaptureMultiActivity.this.getLocalActivityManager());
// 获取手机屏幕的宽高
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
screenWidth = Methods.px2dip(CaptureMultiActivity.this,
displayMetrics.widthPixels);// dp
// 初始化TabHost,依据arr
for (int i=0;i<10;i++) {
View view = LayoutInflater.from(MyTabHostActivity.this).inflate(
R.layout.tab_button, null);
TextView tView = (TextView) view.findViewById(R.id.tv_tabs_tabHost);
tView.setText("tab"+i);
tbProductHost.addTab("tab"+i)
.setIndicator(view).setContent(MyTabHostActivity.this);
updateTab(tbProductHost);//调用方法设置tabWidget选项卡的颜色
tbProductHost.setOnTabChangedListener(new OnTabChangedListener());
}
hScroller = (HorizontalScrollView) findViewById(R.id.hScroller_scan); }
当中Methods类中的px2dip(int tt)方法为:
/**
* px 转 dip
*
* @param context
* @param pxValue
* @return
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
当中updateTab(Tabhost tb)方法主要用于设置tabwidget选项卡的颜色,以及选中时的颜色例如以下:
/**
* 更新Tab标签的颜色,和字体的颜色
*
* @param tabHost
*/
private void updateTab(final TabHost tabHost) {
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
View view = tabHost.getTabWidget().getChildAt(i);
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i)
.findViewById(R.id.tv_tabs_tabHost);
tv.setTextSize(16);
tv.setTypeface(Typeface.SERIF, 2); // 设置字体和风格
if (tabHost.getCurrentTab() == i) {// 选中
view.setBackgroundColor(getResources().getColor(
R.color.color_text_red));// 选中后的背景 #eb037f
tv.setTextColor(this.getResources().getColorStateList(
android.R.color.black));
} else {// 不选中
view.setBackgroundColor(getResources().getColor(
R.color.color_text_yellow));// 非选择的背景 #f8c514
tv.setTextColor(this.getResources().getColorStateList(
android.R.color.white));
}
}
}
当中OnTabChangedListener类:
class OnTabChangedListener implements OnTabChangeListener {
@Override
public void onTabChanged(String tabId) {
tbProductHost.setCurrentTabByTag(tabId);
System.out.println("tabid " + tabId);
System.out.println("curreny after: "
+ tbProductHost.getCurrentTabTag());
updateTab(tbProductHost);
}
}
另外MyTabHostActivity类要实现TabContentFactory, OnGestureListener共计两个接口,并实现里面的方法:
@Override
public View createTabContent(String arg0) {
// 初始化tabHost里面某一个选项卡的内容。能够通过Inflater来载入已经定义好的xml布局文件
//to-do
return view;
} public void flingLeft() {
// 切换选项卡
int currentTab = tbProductHost.getCurrentTab();
if (currentTab != 0) {
currentTab--;
switchTab(currentTab);
}
// 水平滑动
hScrollManagment(true, currentTab);
} public void flingRight() {
// 切换选项卡
int currentTab = tbProductHost.getCurrentTab();
if (currentTab != tbProductHost.getTabWidget().getChildCount()) {
currentTab++;
switchTab(currentTab);
}
// 水平滑动
hScrollManagment(false, currentTab);
}
//用于在切换选项卡时自己主动居中所选中的选项卡的位置
private void hScrollManagment(boolean isToLeft, int currentTab) {
int count = tbProductHost.getTabWidget().getChildCount();
System.out.println("000111:hScrollManagment count=" + count);
if (179 * count > screenWidth) {
int nextPosX = (int) (currentTab + 0.5) * 179 - screenWidth / 2;//此处的179能够自行改动
// hScroller.scrollTo(nextPosX, 0);
hScroller.smoothScrollTo(nextPosX, 0);
}
} private static void switchTab(final int toTab) {
new Thread(new Runnable() {
@Override
public void run() {
tbProductHost.post(new Runnable() {
@Override
public void run() {
tbProductHost.setCurrentTab(toTab);
}
});
}
}).start();
} @Override
public boolean onTouchEvent(MotionEvent event) {
return this.detector.onTouchEvent(event);
} @Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
} @Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e1.getX() - e2.getX() < -120) {
flingLeft();
return true;
} else if (e1.getX() - e2.getX() > 120) {
flingRight();
return true;
} return false;
} @Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub } @Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
} @Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub } @Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
至此。一个完美的tabhost自己定义解决方式完毕。
该资源下载链接:http://download.csdn.net/detail/wanggsx20080817/8295671
版权声明:本文博客原创文章,博客,未经同意,不得转载。
android--解--它们的定义tabhost(动态添加的选项+用自己的主动性横向滑动标签+手势切换标签页和内容特征)的更多相关文章
- 动态添加select选项空选项问题
问题:动态添加校区选项的数据的时候,总是多添加一项空白的数据. 动态添加代码如下: 网上找到的原因:因为在option中有标签没有闭合,所以导致浏览器认为是两个option, 所以只需要给这个标签添加 ...
- 动态添加试题选项按钮 radioButton(一)
最近在做WebView加载试题的功能,但是选项按钮如果放的WebView中,点击时反应很慢.于是把选项用原生的RadioButton,而试题题目和答案放在WebView中.但是选项的个数不确定,所以需 ...
- Android开发之自己定义TabHost文字及背景(源码分享)
使用TabHost 能够在一个屏幕间进行不同版面的切换,而系统自带的tabhost界面较为朴素,我们应该怎样进行自己定义改动优化呢 MainActivity的源码 package com.dream. ...
- Android笔记(六十一)动态添加组件
想要一个功能,点击按钮,可以在已有的布局上,新添加一组组件. 动态的创建组件,本质上还是创建组件,只不过是在程序中根据逻辑来创建.大致步骤是找到要创建控件的位置,然后将要创建的组件添加进去. 看代码: ...
- 利用js给datalist或select动态添加option选项
<!DOCTYPE html> <html> <head> <title>鼠标点击时加载</title> <script type=& ...
- 三. 动态添加option选项
- Android动态添加Device Admin权限
/********************************************************************** * Android动态添加Device Admin权限 ...
- Android-自定义TabHost
效果图: 布局代码相关: <!-- 自定义简单的TabHost选项卡 --> <LinearLayout xmlns:android="http://schemas.and ...
- 怎样在不对控件类型进行硬编码的情况下在 C#vs 中动态添加控件
文章ID: 815780 最近更新: 2004-1-12 这篇文章中的信息适用于: Microsoft Visual C# .NET 2003 标准版 Microsoft Visual C# .NET ...
随机推荐
- C日常语言实践中小(四)——勇者斗恶龙
勇者斗恶龙 愿你的国有n龙的头,你想聘请骑士杀死它(全部的头). 村里有m个骑士能够雇佣,一个能力值为x的骑士能够砍掉恶龙一个致敬不超过x的头,且须要支付x个金币. 怎样雇佣骑士才干砍掉恶龙的全部头, ...
- 用友财务总帐(GL)模BI数据ETL分析
业务需求,如下面的: 现在用友总帐一家公司BI分析案例. /* Sql Server2012使用作业设置定时任务,为了保证有一天运行时间 */ /* 意temp1表里一定要保证要有记录,否则以temp ...
- 【工具】JAVA 在单元读取文件并比较
package test20140709; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; ...
- github jekyll site不再使用Maruku由于Markdown翻译员,但kramdown
今天写了一篇博客,之push至jekyll site on github在,发现总是错的,例如,下面的电子邮件消息: The page build completed successfully, bu ...
- Mac OS X 在捕捉AppLAN通信包
Mac OS X 在捕捉AppLAN通信包 一.拍摄模式 由于工作关系.经常要分析App wifi通讯协议.我的开发电脑是Mac Book. 大体有例如以下几种模式: + App与server进行ht ...
- 16.怎样自学Struts2之Struts2异常处理[视频]
16.怎样自学Struts2之Struts2异常处理[视频] 之前写了一篇"打算做一个视频教程探讨怎样自学计算机相关的技术",优酷上传不了,仅仅好传到百度云上: http://pa ...
- 第七章——DMVs和DMFs(1)
原文:第七章--DMVs和DMFs(1) 简介: 从SQLServer2005开始,微软引入了一个名叫DMO(动态管理对象)的新特性,DMO可以分为DMFs(Dynamic Manage Functi ...
- 查看SQLServer 代理作业的历史信息
原文:查看SQLServer 代理作业的历史信息 不敢说众所周知,但是大部分人都应该知道SQLServer的代理作业情况都存储在SQLServer5大系统数据库(master/msdb/model/t ...
- EJB(一)认识ejb
什么是ejb? 相同仍旧在这个系列博客之处,谈谈对ejb的认识和理解. sun微公司对于ejb的定义大体是这种,ejb是一套用于开发和部署分布式组件的的架构.採用ejb的架构应用能够是 ...
- AndroidMainifest标签说明2——<activity>
格公式: <activity android:allowTaskReparenting=["true" | "false"] android:always ...