1、TabWidget 的 layout文件

<?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">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/background"> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout> <TextView
android:id="@+id/intervalText1"
android:layout_width="match_parent"
android:layout_height="@dimen/interval"
android:background="@color/hint_title_background"
android:layout_above="@android:id/tabs"/> <TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="@dimen/tabs_height"
android:background="@color/white"
android:orientation="horizontal">
</TabWidget> </LinearLayout>
</TabHost>

2、tab布局的layout文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginTop="@dimen/tabs_interval"
android:orientation="vertical"
android:background="@color/white">
<ImageView
android:id="@+id/tab_icon"
android:layout_width="@dimen/tabs"
android:layout_height="@dimen/tabs"
android:scaleType="fitCenter"/>
<TextView
android:id="@+id/tab_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/tabs_interval"
android:textColor="@drawable/main_tab_textcolor"
android:textSize="@dimen/text_size3"/> </LinearLayout>

3、MainActivity

public class MainActivity extends TabActivity {

    private static final String TAB_SALE = "SALE";
private static final String TAB_CART = "CART";
private static final String TAB_REPORT = "REPORT";
private static final String TAB_SETUP = "SETUP"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.maintabs); TabHost tabHost = getTabHost(); //first tab
tabHost.addTab(tabHost.newTabSpec(TAB_SALE)
.setIndicator(prepareTabView(TAB_SALE))
.setContent(new Intent(this, SaleActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
//second tab
tabHost.addTab(tabHost.newTabSpec(TAB_CART)
.setIndicator(prepareTabView(TAB_CART))
.setContent(new Intent(this, CartActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
//third tab
tabHost.addTab(tabHost.newTabSpec(TAB_REPORT)
.setIndicator(prepareTabView(TAB_REPORT))
.setContent(new Intent(this, ReportActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
//forth tab
tabHost.addTab(tabHost.newTabSpec(TAB_SETUP)
.setIndicator(prepareTabView(TAB_SETUP))
.setContent(new Intent(this, SetupActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))); tabHost.setCurrentTab(0);//设置当前的选项卡,这里为Tab1
} //自定义 标签按钮
private View prepareTabView(String text) {
View view = LayoutInflater.from(this).inflate(R.layout.main_tab_view, null);
ImageView imageView = (ImageView) view.findViewById(R.id.tab_icon);
imageView.setBackground(getDrawable(text));
TextView textView = (TextView) view.findViewById(R.id.tab_text);
textView.setText(text);
return view;
} private Drawable getDrawable(String tabLabel){
Drawable backgroundDrawable = null;
if (tabLabel.equals(TAB_SALE)) {
backgroundDrawable = getResources().getDrawable(R.drawable.tab_sale);
} else if (tabLabel.equals(TAB_CART)) {
backgroundDrawable = getResources().getDrawable(R.drawable.tab_cart);
} else if (tabLabel.equals(TAB_REPORT)) {
backgroundDrawable = getResources().getDrawable(R.drawable.tab_report);
} else {
backgroundDrawable = getResources().getDrawable(R.drawable.tab_setup);
}
return backgroundDrawable;
}
}

4、tab切换时图标改变

由于四个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/tab_sale_h"></item>
<item android:state_selected="false" android:drawable="@drawable/tab_sale_n"></item>
</selector>

Android studio 中的TabWidget的更多相关文章

  1. android studio 中移除module和恢复module

    一.移除Android Studio中module 在Android Studio中想要删除某个module时,在Android Studio中选中module,右键发现没有delete,如图: An ...

  2. Android Studio中Button等控件的Text中字符串默认大写的解决方法

    初学Android的时候,在Android Studio中xml里面添加一个Button.EditText等控件后,它的Text总是会显示大写,即使你输入的字符串是小写也不行,控制字符串大小写的属性是 ...

  3. .Net程序员之不学Java做安卓开发:Android Studio中的即时调试窗口

    对学.Net的人来说,JAVA开发是一场噩梦. .net中的即时窗口,调试时直接在里面写代码,对程序中的各种方法/属性进行调用,很方便. Android Studio中找了好久,参考如下网址,也有类似 ...

  4. 如何将Eclipse中的项目迁移到Android Studio 中

    如何将Eclipse中的项目迁移到Android Studio 中 如果你之前有用Eclipse做过安卓开发,现在想要把Eclipse中的项目导入到Android Studio的环境中,那么首先要做的 ...

  5. Android开发的小技巧,在Android Studio中使用Designtime Layout Attributes

    在编写xml文件时,为了预览效果,经常会使用默认填上一些内容,比如TextView时,随便写上一个text <TextView ... android:text="Name:" ...

  6. 在android studio 中使用applicationid的问题

    现在我需要对项目app的某个功能做性能测试,主要测试耗电量的多少. 1.我想到的方式是,我需要在同一台手机测试,同一个应用,需要安装在手机两次,第二次安装不覆盖第一次的安装. 在android stu ...

  7. Android studio 中的配置编译错误总结

    1.编译Andorid 工程的时候,有时候出现gradle 报下面的错误. Error:(1, 0) Cause: com/android/build/gradle/LibraryPlugin : U ...

  8. Android Studio中清单文件改versionCode和versionName没效果的原因

    在Android Studio中,项目的versionCode 和versionName 的控制不是在AndroidManifest.xml清单文件中更改的,而是在项目的build.gradle中更改 ...

  9. android studio中如何设置注释模板

    在开发程序的时候,我们一般都会给文件自动添加上一些关于文件的注释信息,比如开发者的名字,开发的时间,开发者的联系方式等等.那么在android studio中该如何设置呢? 工具/原料   andro ...

随机推荐

  1. SpringMVC工作原理示意图

    上面的是springMVC的工作原理图: 1.客户端发出一个http请求给web服务器,web服务器对http请求进行解析,如果匹配DispatcherServlet的请求映射路径(在web.xml中 ...

  2. gentoo 建立本地软件库并安装软件 Custom repository

    参考 Handbook:AMD64/Portage/CustomTree https://wiki.gentoo.org/wiki/Handbook:AMD64/Portage/CustomTree# ...

  3. 【Noip模拟 20160929】划区灌溉

    题目描述 约翰的奶牛们发现山脊上的草特别美味.为了维持草的生长,约翰打算安装若干喷灌器. 为简化问题,山脊可以看成一维的数轴,长为L(1≤L≤1,000,000)L(1≤L≤1,000,000),而且 ...

  4. 使用jQuery+huandlebars循环中索引(@index)使用技巧(访问父级索引)

    兼容ie8(很实用,复制过来,仅供技术参考,更详细内容请看源地址:http://www.cnblogs.com/iyangyuan/archive/2013/12/12/3471227.html) & ...

  5. git command line 提交代码

    echo "# spring-boot-apollo-demo" >> README.md git init git add README.md git commit ...

  6. canvas绘制气泡

    思路:使用Math.random()函数绘制是个不同位置,大小,颜色的圆形,然后设置定时器,前一个状态用一个与画布相同颜色的背景图片进行覆盖,改变圆形的位置,每次改变都是在这张空白的背景图片上面重新进 ...

  7. Java虚拟机 内存区域划分

    (图片来自https://www.cnblogs.com/whgk/p/6138522.html) 先从线程私有区开始介绍 虚拟机栈 Java虚拟机栈是由一个个栈帧组成的,当一个方法被调用时,代表这个 ...

  8. 初学c# -- 学习笔记(9) 关于SQL2008

    在做一个局域网的类似网盘的学习练习,服务端差不多了,在改bug.用vlc的dll做的全格式视频.音频预览在线播放下载等等. 在做服务端也遇到了一些问题,走了好多弯路. 开始把上传的视频.音频.图像.文 ...

  9. python 迭代器生成

    博客:http://www.cnblogs.com/alex3714/articles/5765046.html 列表生成式 [i*2 for i in range(10)] #创建时就生成,不调用也 ...

  10. 9.22 keep studying

    项目要换用element组件了. element [http://element.eleme.io/1.4/#/zh-CN]是饿了么前端出品的一套基于Vue2.0的组件库,今天看了一下,确实简单好看. ...