在使用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设置背景和字体的更多相关文章

  1. 深入理解gradle编译-Android基础篇

    深入理解gradle编译-Android基础篇 导读 Gradle基于Groovy的特定领域语言(DSL)编写的一种自动化建构工具,Groovy作为一种高级语言由Java代码实现,本文将对Gradle ...

  2. android基础篇学习心得

    android技术中,线程.进程.JNI.IPC和各个小框架结构是基本功.在跟随高焕堂老师的android程序猿到架构师之路系列视频中 学习完基础篇之后,颇有些心得,记录下来. android开发就是 ...

  3. Android基础TOP5_5:设置没有标题栏而且用系统壁纸当背景的界面

    在res/values目录下的style.xml设置如下 <style name="AppBaseTheme" parent="android:Theme.Wall ...

  4. Android 基础篇(二)

    ADB进程 adb指令 adb install xxx.apk adb uninstall 包名 adb devices adb start-server adb kill-server adb sh ...

  5. 安卓工作室android studio 美化 ,设置背景图片。

    作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com E-mail: 313134555 @qq.com sexy Editor 点击file-> ...

  6. Android基础TOP2:单机按钮改变字体颜色

    ---恢复内容开始--- Activity: <TextView android:id="@+id/t1" android:textSize="30dp" ...

  7. Android基础篇(一)

    Android体系结构介绍 Android是一个移动开发平台,层次结构:操作系统(OS).中间件(Middle Ware).应用程序(Application) 具体: 操作系统(OS)-->各种 ...

  8. android基础篇------------java基础(12)(多线程操作)

    <一>基本概念理解 1.什么是进程? 进程就是在某种程度上相互隔离,独立运行的程序.一般来说,系统都是支持多进程操作的,这所谓的多进程就是让系统好像同时运行多个程序. 2.什么是线程呢? ...

  9. CSS设置背景透明字体不透明

    写CSS时给容器设置透明度的时候如果使用background-color: #000000; opacity: 0.5;这时会出现容器里的文字也跟着透明.解决办法是不用十六进制的色值和透明度分开写,使 ...

随机推荐

  1. jquery的几种ajax方式对比

    jquery的几种ajax方式对比 jquery的ajax方式有如下几种: 1.   $.post(url,params,callback); 2.   $.getJSON(url,params,ca ...

  2. requests(一): 发送一个json格式的post请求

    今天给一位同学解决post发送数据格式为json格式的请求,顺便确认一下问题归属. 背景: 用postman工具发送一个数据格式为json的请求,得到了服务器的响应. 用python的requests ...

  3. Nginx报错:upstream timed out (110: Connection timed out)和client intended to send too large body【转】

    nginx日志报错 2018/01/26 16:36:49 [error] 23327#0: *54953 upstream timed out (110: Connection timed out) ...

  4. XSS代码注入框架

    首先需要了解一下几点: 1.浏览器中Javascript变量的生命周期 Javascript变量的生命周期并不是你声明这个变量个窗口闭就被回收,只要有引用就会一直持续到浏览器关闭. 2.window对 ...

  5. SqlServer共用表达式(CTE)With As

    共用表表达式(CTE)可以看成是一个临时的结果集,可以再SELECT,INSERT,UPDATE,DELETE,MARGE语句中多次引用. 一好处:使用共用表表达式可以让语句更加清晰简练. 1.可以定 ...

  6. 图学ES6-5.正则的扩展

  7. IScroll5安卓重复点击兼容问题处理

    最近在做移动web开发,使用IScroll 5 的时候出现了设备之间兼容的问题: 情景如下: Android手机:点击滚动区间内的选项时出现点击时间重叠(类似事件冒泡的行为)问题 Apple手机:木有 ...

  8. wfst的compose算法

    介绍一些compose算法,以及这部分的代码实现. 原理部分参考: 走进语音识别中的 WFST(二) 可以看下示例图: 我们先来看一下 Composition 的效果,图(a)和图(b)Composi ...

  9. 【LOJ】#2536. 「CQOI2018」解锁屏幕

    题解 什么破题,看一眼就能想出来\(n^2 2^n\)看了一眼数据范围有点虚,结果跑得飞快= = 处理出\(a[i][j]\)表示从\(i\)到\(j\)经过的点的点集 然后\(f[i][S]\)表示 ...

  10. python程序后台运行的实现

    后台运行work()方法. work.py # -*- coding:utf-8 -*- def work(): print "running" import time time. ...