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. 给tableView设置背景图片

    经常遇到要给tableView设置背景图片的问题,但如果直接设置背景  backgroundView的话,背景图不会显示,原因是  tableView上的cell默认是不透明的颜色,所以解决方法是 让 ...

  2. python:win下将py文件打包成exe

    [环境]windows,正常运行的python文件 1.安装pyinstaller ,cmd下执行以下命令,需看到安装成功界面 pip install pyinstaller 2.cmd中进入要打包的 ...

  3. MATLAB——矩阵排序详解

    <span style="font-size:18px;">(1)B=sort(A) 对一维或二维数组进行升序排序,并返回排序后的数组,当A为二维时,对数组每一列进行排 ...

  4. svn的下载及安装

    什么是SVN: SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS. SVN的下载安装: 下载地址:https: ...

  5. mysql授权远程访问

    mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY '123456' WITH GRANT OPTION; Query O ...

  6. jQuery开发工具

    开发工具:MyEclipse2014 + aptana插件 下载apada 放到MyEclipse的路径   https://segmentfault.com/a/1190000005711923   ...

  7. python argparse(参数解析)模块学习(二)

    转载自:http://www.cnblogs.com/fireflow/p/4841389.html(我去..没转载功能,ctrl + c 和 ctrl + v 得来的,格式有点问题,可去原版看看) ...

  8. 如何配置Java环境变量[转]

    https://jingyan.baidu.com/article/fd8044fa2c22f15031137a2a.html

  9. canvas(四) Gradient- demo

    /** * Created by xianrongbin on 2017/3/9. */ /* strokeStyle 或 fillStyle 属性的值*/ /** * Demo1 创建线性渐变 */ ...

  10. centos 搭建git 服务器

    安装 git yum -y install git 添加git 用户 adduser git 切换到git 用户 su git 在git用户家目录下创建  .ssh文件夹 mkdir .ssh 修改文 ...