Android開發中項目實現一個自定義Tab選項卡功能?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
具體如下:
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("設置",getResources().getDrawable(R.drawable.s))
.setContent(R.id.content));
for (int i =0; i < tabWidget.getChildCount(); i++) {
/**
* 設置高度、寬度,不過寬度由于設置為fill_parent,在此對它沒效果
*/
tabWidget.getChildAt(i).getLayoutParams().height = height;
tabWidget.getChildAt(i).getLayoutParams().width = width;
/**
* 設置tab中標題文字的顏色,不然默認為黑色
*/
final TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(this.getResources().getColorStateList(android.R.color.white));
/**
* 此方法是為了去掉系統默認的色白的底角
*
* 在 TabWidget中mBottomLeftStrip、mBottomRightStrip
* 都是私有變量,但是我們可以通過反射來獲取
*
* 由于還不知道Android 2.2的接口是怎么樣的,現在先加個判斷好一些
*/
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));
}
}
/**
* 當點擊tab選項卡的時候,更改當前的背景
*/
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));
}
}
}});
}
}關于Android開發中項目實現一個自定義Tab選項卡功能問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。