Android程序開發(fā)之自定義設(shè)置TabHost,TabWidget樣式
先看效果:

京東商城底部菜單欄

新浪微博底部菜單欄

本次學(xué)習(xí)效果圖:

第一,主布局文件(啟動頁main.xml,位于res/layout目錄下)代碼
<?xml version="." encoding="utf-"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tabhost"> <LinearLayout android:id="@+id/linear" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content"></TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:id="@+id/tab" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:id="@+id/tab_txt" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:text="你"/> </LinearLayout> <LinearLayout android:id="@+id/tab" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:id="@+id/tab_txt" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:text="我"/> </LinearLayout> <LinearLayout android:id="@+id/tab" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:id="@+id/tab_txt" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:text="他"/> </LinearLayout> <LinearLayout android:id="@+id/tab" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:id="@+id/tab_txt" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:text="我們"/> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost>
第二,創(chuàng)建顯示此TabWidget的布局tabmini.xml(位于res/layout目錄下)
<?xml version="." encoding="utf-"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingTop="dp" android:paddingLeft="dp" android:paddingRight="dp" android:background="#CEC" > <TextView android:id="@+id/tab_label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:textColor="#" android:textStyle="bold" android:background="@drawable/tabmini"/> </RelativeLayout>
第三,在drawable里面創(chuàng)建一個selector,命名tabmini.xml,用來點擊TabHost的一個tab時TextView的變化
<?xml version="." encoding="utf-"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_selected="true" android:drawable="@drawable/add_managebg_down"/> <item android:state_selected="false" android:drawable="@drawable/add_managebg"/> </selector>
第四,java代碼,在Activity里實現(xiàn)TabHost
package com.example.androidtest____meihuatubiao;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View niTab=(View)LayoutInflater.from(this).inflate(R.layout.tabmini, null);
TextView niTxt=(TextView)niTab.findViewById(R.id.tab_label);
niTxt.setText("ni");
View woTab=(View)LayoutInflater.from(this).inflate(R.layout.tabmini, null);
TextView woTxt=(TextView)woTab.findViewById(R.id.tab_label);
woTxt.setText("wo");
View taTab=(View)LayoutInflater.from(this).inflate(R.layout.tabmini, null);
TextView taTxt=(TextView)taTab.findViewById(R.id.tab_label);
taTxt.setText("ta");
View weTab=(View)LayoutInflater.from(this).inflate(R.layout.tabmini, null);
TextView weTxt=(TextView)weTab.findViewById(R.id.tab_label);
weTxt.setText("we");
TabHost tabs=(TabHost)findViewById(R.id.tabhost);
tabs.setup();
tabs.addTab(tabs.newTabSpec("niTab").setContent(R.id.tab).setIndicator(niTab));
tabs.addTab(tabs.newTabSpec("woTab").setContent(R.id.tab).setIndicator(woTab));
tabs.addTab(tabs.newTabSpec("taTab").setContent(R.id.tab).setIndicator(taTab));
tabs.addTab(tabs.newTabSpec("weTab").setContent(R.id.tab).setIndicator(weTab));
}
}
以上內(nèi)容是小編給大家分享的Android程序開發(fā)之自定義設(shè)置TabHost,TabWidget樣式,希望對大家有所幫助!
相關(guān)文章
Android 中CheckBox的isChecked的使用實例詳解
這篇文章主要介紹了Android 中CheckBox的isChecked的使用實例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
詳解Android使用OKHttp3實現(xiàn)下載(斷點續(xù)傳、顯示進度)
本篇文章主要介紹了詳解Android使用OKHttp3實現(xiàn)下載(斷點續(xù)傳、顯示進度),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
Android開發(fā)獲取短信的內(nèi)容并截取短信
本文給大家介紹android開發(fā)獲取短信內(nèi)容并截取短息的相關(guān)內(nèi)容,本文代碼簡單易懂,感興趣的朋友一起學(xué)習(xí)吧2015-12-12
Android利用Intent啟動和關(guān)閉Activity
這篇文章主要為大家詳細介紹了Android利用Intent啟動和關(guān)閉Activity的相關(guān)操作,感興趣的小伙伴們可以參考一下2016-06-06
Android自定義ViewGroup嵌套與交互實現(xiàn)幕布全屏滾動
這篇文章主要為大家介紹了Android自定義ViewGroup嵌套與交互實現(xiàn)幕布全屏滾動效果示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01
Android開發(fā)之模仿微信打開網(wǎng)頁的進度條效果(高仿)
這篇文章主要介紹了Android開發(fā)之模仿微信打開網(wǎng)頁的進度條效果(高仿)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07

