【Android基础篇】TabWidget设置背景和字体
在使用TabHost实现底部导航栏时,底部导航栏的三个导航button无法在布局文件中进行定制。比方设置点击时的颜色、字体的大小及颜色等,这里提供了一个解决的方法。就是在代码里进行定制。
思路是在Activity里给TabHost加入了分页后,在给导航栏TabWidget的导航button逐个加入特效(必须先加入分页。然后才干定制button,加入了一个分页,才会生成一个button)。
以下是布局文件activity_main.xml,包括了TabHost,里面有三个仅仅显示了文字的分页
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.plan.MainActivity"
tools:ignore="MergeRootFrame" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="0.8" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab1" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab2" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab3" />
</LinearLayout>
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
</LinearLayout>
</TabHost>
</RelativeLayout>
以下是MainActivity里的代码:
package com.aiplan_03;
import android.app.ActivityGroup;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
public class MainActivity extends ActivityGroup {
TabHost mTabHost = null;
TabWidget mTabWidget = null; //TabWidget控件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this.getLocalActivityManager());
//获取导航button控件
mTabWidget = mTabHost.getTabWidget();
//加入分页1
mTabHost.addTab(mTabHost.newTabSpec("button1").setContent(
R.id.tab1).setIndicator("btn1"));
//加入分页2
mTabHost.addTab(mTabHost.newTabSpec("button2").setContent(
R.id.tab2).setIndicator("btn2"));
//加入分页3
mTabHost.addTab(mTabHost.newTabSpec("button3").setContent(
R.id.tab3).setIndicator("btn3"));
Log.d("button数",Integer.toString(mTabWidget.getChildCount()));
//逐个button加入特效
for(int i=0;i<mTabWidget.getChildCount();i++){
//换字体颜色
TextView tv = (TextView)
mTabWidget.getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(Color.rgb(255, 255, 255));
//设置背景图
mTabWidget.getChildAt(i).setBackgroundResource(
R.drawable.tabwidget_selector);
}
}
}
把导航button的字体换成了白色,给导航button的背景加入了一个selector选择器。以下是选择器代码:
tabwidget_selector.xml。需放到drawable目录下
<?
xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_selected="false"
android:drawable="@color/tabwidget_unselected"
/>
<item
android:state_selected="true"
android:drawable="@color/tabwidget_selected" />
</selector>
里面用到了两个颜色。以下是color.xml,需放到values目录下
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<color name="tabwidget_selected">#ff2222</color>
<color name="tabwidget_unselected">#000000</color>
</resources>
最后的效果图例如以下:
【Android基础篇】TabWidget设置背景和字体的更多相关文章
- 深入理解gradle编译-Android基础篇
深入理解gradle编译-Android基础篇 导读 Gradle基于Groovy的特定领域语言(DSL)编写的一种自动化建构工具,Groovy作为一种高级语言由Java代码实现,本文将对Gradle ...
- android基础篇学习心得
android技术中,线程.进程.JNI.IPC和各个小框架结构是基本功.在跟随高焕堂老师的android程序猿到架构师之路系列视频中 学习完基础篇之后,颇有些心得,记录下来. android开发就是 ...
- Android基础TOP5_5:设置没有标题栏而且用系统壁纸当背景的界面
在res/values目录下的style.xml设置如下 <style name="AppBaseTheme" parent="android:Theme.Wall ...
- Android 基础篇(二)
ADB进程 adb指令 adb install xxx.apk adb uninstall 包名 adb devices adb start-server adb kill-server adb sh ...
- 安卓工作室android studio 美化 ,设置背景图片。
作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com E-mail: 313134555 @qq.com sexy Editor 点击file-> ...
- Android基础TOP2:单机按钮改变字体颜色
---恢复内容开始--- Activity: <TextView android:id="@+id/t1" android:textSize="30dp" ...
- Android基础篇(一)
Android体系结构介绍 Android是一个移动开发平台,层次结构:操作系统(OS).中间件(Middle Ware).应用程序(Application) 具体: 操作系统(OS)-->各种 ...
- android基础篇------------java基础(12)(多线程操作)
<一>基本概念理解 1.什么是进程? 进程就是在某种程度上相互隔离,独立运行的程序.一般来说,系统都是支持多进程操作的,这所谓的多进程就是让系统好像同时运行多个程序. 2.什么是线程呢? ...
- CSS设置背景透明字体不透明
写CSS时给容器设置透明度的时候如果使用background-color: #000000; opacity: 0.5;这时会出现容器里的文字也跟着透明.解决办法是不用十六进制的色值和透明度分开写,使 ...
随机推荐
- Madgwick IMU Filter
论文链接:http://202.114.96.204/cache/13/03/x-io.co.uk/35c82431852f2aa7d0feede9dc138626/madgwick_internal ...
- 如何生成能在没有安装opencv库及vs2010环境的电脑上运行的exe文件
项目基本算法已经完成,甲方需要一个可以运行的demo.目前,程序能在自己的电脑上正常运行.移植到其他win7系统上,运行失败. 寻找各种解决办法,baidu找到两个办法: 1.使用静态链接的方法,这种 ...
- csfb
SELECT CSDBTOGSMALLSuccessRate,CSFBTOGSMMODelay,CSFBTOGSMMODropRate,CSFBTOGSMMOFRStartCount,CSFBTOGS ...
- 激活Window和office工具
激活Window和office工具: 第一种工具(已使用工具激活microsoft office professional plus 2013版本): 暴风激活工具(暴风激活工具 ...
- SpringMvc定时器任务
在最近的工作中,涉及到一个定时任务,由于以前对springMVC使用较少,所以,上网找了一点资料.这个demo感觉挺好,推荐给大家. 使用到的JAR文件: aopalliance-1.0.jarcom ...
- 网络抓包神器-Charles使用指南
http://blog.csdn.net/liulanghk/article/details/46342205 目录 概述 安装 显示模式 PC端抓包 移动应用抓包 其他技能 charles使用问题汇 ...
- .net4.0切换2.0时,SplitContainer”的对象强制转换为类型
问 题:将dotnet framework 4.0 切换到2.0时,编译没有问题,在运行时出现如下错误:System.InvalidCastException: 无法将类型为“System.Windo ...
- Densenet-Tensorflow
在寻找densnet网络的时候,我发现了一个结构清晰完整的网络代码,在此作备份. https://github.com/taki0112/Densenet-Tensorflow Densenet-Te ...
- spring-service.xml 模板
ssm模板 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:/ ...
- 微信小程序《沈航二手书》
微信小程序<沈航二手书> 0x01. 利益相关者 利益相关者:是指与客户有一定利益关系的个人或组织群体,可能是客户内部的(如雇员),也可能是客户外部的(如供应商或压力群体). 根据相关利 ...