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樣式,希望對大家有所幫助!
- Android開發(fā)之TabHost選項卡及相關(guān)疑難解決方法
- Android TabHost選項卡標(biāo)簽圖標(biāo)始終不出現(xiàn)的解決方法
- android FragmentTabhost實現(xiàn)導(dǎo)航分頁
- Android 中 TabHost與ViewPager結(jié)合實現(xiàn)首頁導(dǎo)航效果
- Android組件必學(xué)之TabHost使用方法詳解
- Android組件TabHost實現(xiàn)頁面中多個選項卡切換效果
- android TabHost(選項卡)的使用方法
- Android TabHost如何實現(xiàn)頂部選項卡
相關(guān)文章
Android registerForActivityResult動態(tài)申請權(quán)限案例詳解
這篇文章主要介紹了Android registerForActivityResult動態(tài)申請權(quán)限案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09android教程使用webview訪問https的url處理sslerror示例
這篇文章主要介紹了android教程使用webview訪問https的url處理sslerror示例,大家參考使用吧2014-01-01關(guān)于Android的 DiskLruCache磁盤緩存機制原理
DiskLruCache是一種管理數(shù)據(jù)存儲的技術(shù),單從Cache的字面意思也可以理解到,"Cache","高速緩存";今天我們來從源碼上分析下DiskLruCache;關(guān)于Android LruCache的緩存機制原理,需要的朋友可以參考下面文章的具體內(nèi)容2021-09-09Android應(yīng)用的LinearLayout中嵌套RelativeLayout的布局用法
這篇文章主要介紹了Android應(yīng)用的LinearLayout中嵌套RelativeLayout的布局用法,文后還給出了線性布局中一些組件位置的調(diào)試經(jīng)驗,需要的朋友可以參考下2016-04-04手把手教學(xué)Android用jsoup解析html實例
本篇文章主要介紹了手把手教學(xué)Android用jsoup解析html實例,jsoup 是一款Java 的HTML解析器。具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06android-獲取網(wǎng)絡(luò)時間、獲取特定時區(qū)時間、時間同步的方法
本篇文章主要介紹了android-獲取網(wǎng)絡(luò)時間、獲取特定時區(qū)時間、時間同步,小編覺得不錯,現(xiàn)在就分享給大家,有興趣的可以了解一下。2016-12-12