【Android基础篇】TabWidget设置背景和字体

在使用TabHost实现底部导航栏时,底部导航栏的三个导航按钮无法在布局文件里进行定制,比如设置点击时的颜色、字体的大小及颜色等,这里提供了一个解决办法,就是在代码里进行定制。

思路是在Activity里给TabHost添加了分页后,在给导航栏TabWidget的导航按钮逐个添加特效(必须先添加分页,然后才能定制按钮,添加了一个分页,才会生成一个按钮)。

下面是布局文件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());
         //获取导航按钮控件
         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("按钮数",Integer.toString(mTabWidget.getChildCount()));
         //逐个按钮添加特效
         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);
         }

    }
}

把导航按钮的字体换成了白色,给导航按钮的背景添加了一个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>

最后的效果图如下:

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-29 12:35:54

【Android基础篇】TabWidget设置背景和字体的相关文章

Android基础篇之Android快速入门--你必须要知道的基础

Android快速入门 1. 搭建开发环境 >解压压缩文件,得到:①Android SDK   (类似于JDK)② Eclipse  ③ADT >配置两个path环境变量:D:\adt-bundle-windows-x86\sdk\platform-tools:D:\adt-bundle-windows-x86\sdk\tools >配置基本的Eclipse的设置: 调整字体大小,字符集,配置android sdk的位置 >创建模拟器: 2. 创建第一个Android项目: Hel

android基础篇学习心得

android技术中,线程.进程.JNI.IPC和各个小框架结构是基本功.在跟随高焕堂老师的android程序猿到架构师之路系列视频中 学习完基础篇之后,颇有些心得,记录下来. android开发就是app开发吗?曾听过很多人说android学习很简单,做个app轻松就上手了.我一直觉得不以为然,许多程序员做app开发时,心中大致只有四大组件.各种布局.数据库和一些常用控件.对于各小框架.线程间通信.绑定服务.java与c的对接等基本原理并不清楚,也没必要弄清楚. 最近学校搞了一个实训,要求做一

【Android基础篇】TabHost实现底部导航栏

在App应用中,导航栏往往是用于解决功能分块的最佳控件,而底部导航栏更是导航栏中最常用的,因为它位于屏幕底部,用户操作起来会很方便. 下面介绍一下使用Android控件TabHost实现底部导航栏的方法. TabHost可以在控件库里直接拖到页面上,非常方便,但拖出来的是顶部导航栏,如下图所示: 到这里就可以开始实现底部导航栏了,我们首先转到它的XML布局代码里,然后修改成下面这样: <FrameLayout xmlns:android="http://schemas.android.co

【Android基础】动态设置颜色值的方法

在Android里我们通常都是在Xml里设置字体或者图片等等的颜色值,但是有些时候我们要用到动态的设置颜色值(说白了就是在代码里设置颜色值),这个时候可能有些朋友不太清除这方面,所以我总结了以下两个方式(暂时): 1.通过Color这个API获取RGB颜色值,例: tvContent.setTextColor(Color.parseColor("#969696")); 2.通过getResources函数获取到Resources,接着用getColor方法获取对应的color文件下的颜

【Android基础篇】AutoCompleteTextView和MultiAutoCompleteTextView

从名称上可看出来,这两个控件都是用于输入信息的TextView,AutoComplete已表明这两个控件内容输入都是自动完成的.区别在于一个是Multi,一个不是,具体的区别可通过下面的内容看出来.下面分别介绍着两个控件的使用. AutoCompleteTextView 功能 动态匹配输入的内容,如搜索引擎在输入框输入信息时,会有一个下拉列表显示与当前输入内容有关的信息. 控件特有属性 如同width.height等属性是控件共有属性,下面介绍AutoCompleteTextView特有的属性:

从0开始不断温习,Android基础篇

(^▽^)经常发现学着学着,由于学习的东西越来越多,接触的东西越来越多,逐渐的吧自己的最基础的东西忘得差不多了(o(╥﹏╥)o我也差不多忘了很多东西了)发现越优秀的人 越注重细节,基础更加扎实和巩固 分享一下自己整理的面试学习路线 请查看完整的PDF版(更多完整项目下载.未完待续.源码.图文知识后续上传github.)可以点击关于我联系我获取完整PDF(VX:mm14525201314) 一.Activity 是什么? Activity 实际上只是一个与用户交互的接口而已 二.Activity

【android基础篇】ContextProvider基本认识

ContentProvider即内容提供者.安卓的四大组件之一.I,ContextProvider的用途 对于市面上有很多软件有这么一些功能,比如:读取联系人,短信备份等.而系统的短信以及联系人都是以数据库文件形式存储着.但是这些文件对于其他访问者的权限是不可读不可写.那么那些短信备份,读取联系人这些功能又是怎么实现的呢?其实这里,靠的就是内容提供者. 可以把内容提供者想象成证监会和股民之间的中间人,这个中间人可能是证监会某个内部人员的亲戚朋友,它可以提供有效的信息给股民.但是如果证监会遵纪守法

【Android基础篇】使用ExpandableListView实现自定义的下拉列表

1. ExpandableListView简介 下拉列表(可扩展的列表控件)在App应用非常常见,在Android开发中是我们必须掌握的一个控件,下面就来介绍一下ExpandableListView这个控件的开发. ExpandableListView分为组列表项和子列表项,单击组列表项,会显示这组里所有的子列表项.和ListView一样,它也是通过Adapter数据适配器完成数据与显示的衔接,但它使用的另一种接口:ExpandableListAdapter. 今天我们要做的是实现一个继承它的父

【android基础篇】自定义控件实现一些比较常见的功能

I,我要实现的效果 如图所示: 当下拉时,显示下拉刷新.松开后,显示松开刷新,接着正在刷新. II,准备工作 1)需要自定义ListView,写一个类继承ListView.并在ListView中添加一些数据. 1 private void initView() { 2 listData = new ArrayList<String>(); 3 for (int i = 0; i <30; i++) { 4 listData.add("我是来自仙桃的西藏哥,我真的很爱骑行....