您好,登錄后才能下訂單哦!
Android開發(fā)中項(xiàng)目實(shí)現(xiàn)一個(gè)自定義Tab選項(xiàng)卡功能?針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。
具體如下:
import android.app.TabActivity; import android.content.Intent; import android.os.Bundle; import android.widget.*; import android.widget.TabHost.OnTabChangeListener; import android.os.Build; import android.view.View; import java.lang.reflect.Field; import android.view.LayoutInflater; public class testTabActivity extends TabActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); int width =45; int height =48; final TabHost tabs = getTabHost(); final TabWidget tabWidget = tabs.getTabWidget(); Field mBottomLeftStrip; Field mBottomRightStrip; LayoutInflater.from(this).inflate(R.layout.tab_views, tabs.getTabContentView(), true); tabs.addTab(tabs.newTabSpec("first tab") .setIndicator("信息",getResources().getDrawable(R.drawable.m)) .setContent(new Intent(testTabActivity.this,OneActivty.class)) ); tabs.addTab(tabs.newTabSpec("second tab") .setIndicator("收藏",getResources().getDrawable(R.drawable.n)) .setContent(R.id.content)); tabs.addTab(tabs.newTabSpec("second tab") .setIndicator("設(shè)置",getResources().getDrawable(R.drawable.s)) .setContent(R.id.content)); for (int i =0; i < tabWidget.getChildCount(); i++) { /** * 設(shè)置高度、寬度,不過寬度由于設(shè)置為fill_parent,在此對它沒效果 */ tabWidget.getChildAt(i).getLayoutParams().height = height; tabWidget.getChildAt(i).getLayoutParams().width = width; /** * 設(shè)置tab中標(biāo)題文字的顏色,不然默認(rèn)為黑色 */ final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title); tv.setTextColor(this.getResources().getColorStateList(android.R.color.white)); /** * 此方法是為了去掉系統(tǒng)默認(rèn)的色白的底角 * * 在 TabWidget中mBottomLeftStrip、mBottomRightStrip * 都是私有變量,但是我們可以通過反射來獲取 * * 由于還不知道Android 2.2的接口是怎么樣的,現(xiàn)在先加個(gè)判斷好一些 */ if (Float.valueOf(Build.VERSION.RELEASE) <= 2.1) { try { mBottomLeftStrip = tabWidget.getClass().getDeclaredField ("mBottomLeftStrip"); mBottomRightStrip = tabWidget.getClass().getDeclaredField ("mBottomRightStrip"); if(!mBottomLeftStrip.isAccessible()) { mBottomLeftStrip.setAccessible(true); } if(!mBottomRightStrip.isAccessible()){ mBottomRightStrip.setAccessible(true); } mBottomLeftStrip.set(tabWidget, getResources().getDrawable (R.drawable.no)); mBottomRightStrip.set(tabWidget, getResources().getDrawable (R.drawable.no)); } catch (Exception e) { e.printStackTrace(); } } else { /** * 不做任何處理 */ } View vvv = tabWidget.getChildAt(i); if(tabs.getCurrentTab()==i){ vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_button)); } else { vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.bar)); } } /** * 當(dāng)點(diǎn)擊tab選項(xiàng)卡的時(shí)候,更改當(dāng)前的背景 */ tabs.setOnTabChangedListener(new OnTabChangeListener(){ @Override public void onTabChanged(String tabId) { // TODO Auto-generated method stub for (int i =0; i < tabWidget.getChildCount(); i++) { View vvv = tabWidget.getChildAt(i); if(tabs.getCurrentTab()==i){ vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_button)); } else { vvv.setBackgroundDrawable(getResources().getDrawable(R.drawable.bar)); } } }}); } }
關(guān)于Android開發(fā)中項(xiàng)目實(shí)現(xiàn)一個(gè)自定義Tab選項(xiàng)卡功能問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。