Android程序開(kāi)發(fā)之自定義設(shè)置TabHost,TabWidget樣式
先看效果:
京東商城底部菜單欄
新浪微博底部菜單欄
本次學(xué)習(xí)效果圖:
第一,主布局文件(啟動(dòng)頁(yè)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)建一個(gè)selector,命名tabmini.xml,用來(lái)點(diǎn)擊TabHost的一個(gè)tab時(shí)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里實(shí)現(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程序開(kāi)發(fā)之自定義設(shè)置TabHost,TabWidget樣式,希望對(duì)大家有所幫助!
- Android開(kāi)發(fā)之TabHost選項(xiàng)卡及相關(guān)疑難解決方法
- Android TabHost選項(xiàng)卡標(biāo)簽圖標(biāo)始終不出現(xiàn)的解決方法
- android FragmentTabhost實(shí)現(xiàn)導(dǎo)航分頁(yè)
- Android 中 TabHost與ViewPager結(jié)合實(shí)現(xiàn)首頁(yè)導(dǎo)航效果
- Android組件必學(xué)之TabHost使用方法詳解
- Android組件TabHost實(shí)現(xiàn)頁(yè)面中多個(gè)選項(xiàng)卡切換效果
- android TabHost(選項(xiàng)卡)的使用方法
- Android TabHost如何實(shí)現(xiàn)頂部選項(xiàng)卡
相關(guān)文章
Android自定義View實(shí)現(xiàn)五子棋小游戲
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)五子棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11Android registerForActivityResult動(dòng)態(tài)申請(qǐng)權(quán)限案例詳解
這篇文章主要介紹了Android registerForActivityResult動(dòng)態(tài)申請(qǐng)權(quán)限案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09android教程使用webview訪問(wèn)https的url處理sslerror示例
這篇文章主要介紹了android教程使用webview訪問(wèn)https的url處理sslerror示例,大家參考使用吧2014-01-01關(guān)于Android的 DiskLruCache磁盤(pán)緩存機(jī)制原理
DiskLruCache是一種管理數(shù)據(jù)存儲(chǔ)的技術(shù),單從Cache的字面意思也可以理解到,"Cache","高速緩存";今天我們來(lái)從源碼上分析下DiskLruCache;關(guān)于Android LruCache的緩存機(jī)制原理,需要的朋友可以參考下面文章的具體內(nèi)容2021-09-09Android應(yīng)用的LinearLayout中嵌套R(shí)elativeLayout的布局用法
這篇文章主要介紹了Android應(yīng)用的LinearLayout中嵌套R(shí)elativeLayout的布局用法,文后還給出了線性布局中一些組件位置的調(diào)試經(jīng)驗(yàn),需要的朋友可以參考下2016-04-04Android轉(zhuǎn)場(chǎng)動(dòng)畫(huà)深入分析探究
對(duì)于一個(gè)動(dòng)畫(huà)而言,它是由多個(gè)分鏡頭組成的,而轉(zhuǎn)場(chǎng)就是分鏡之間銜接方式。轉(zhuǎn)場(chǎng)的主要目的,就是為了讓鏡頭與鏡頭之間過(guò)渡的更加自然,讓動(dòng)畫(huà)更加連貫,例如兩個(gè)頁(yè)面切換之間的銜接動(dòng)畫(huà)2022-10-10手把手教學(xué)Android用jsoup解析html實(shí)例
本篇文章主要介紹了手把手教學(xué)Android用jsoup解析html實(shí)例,jsoup 是一款Java 的HTML解析器。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06android-獲取網(wǎng)絡(luò)時(shí)間、獲取特定時(shí)區(qū)時(shí)間、時(shí)間同步的方法
本篇文章主要介紹了android-獲取網(wǎng)絡(luò)時(shí)間、獲取特定時(shí)區(qū)時(shí)間、時(shí)間同步,小編覺(jué)得不錯(cuò),現(xiàn)在就分享給大家,有興趣的可以了解一下。2016-12-12