由於在所做的項目中的圖片資源都是從網上摳的,所以造成了,在TabWidget中圖片過大,從而覆蓋掉了裡面的文字,最開始的效果
在網上眾多的搜尋結果中,網上大部分將的都是有關怎麼更改字型的大小及顏色的問題,代碼如下 設定tabHost 中的tabs的字型顏色、大小等;
TabWidget tabWidget=this.getTabWidget();
for (int i = 0; i < tabWidget.getChildCount(); i++) {
TextView tv=(TextView)tabWidget.getChildAt(i).findViewById(android.R.id.title);
tv.setGravity(BIND_AUTO_CREATE);
tv.setPadding(10, 10,10, 10);
tv.setTextSize(16);//設定字型的大小;
tv.setTextColor(Color.WHITE);//設定字型的顏色;
//擷取tabs圖片;
ImageView iv=(ImageView)tabWidget.getChildAt(i).findViewById(android.R.id.icon);
} 根據上面的例子我們估計會採用兩種方式來解決的問題1、for (int i = 0; i < tabWidget.getChildCount(); i++) {
//擷取tabs圖片;
ImageView iv=(ImageView)tabWidget.getChildAt(i).findViewById(android.R.id.icon);
iv.setMaxHeight(30);
iv.setMaxWidth(30);
}這樣我們發現還是沒有一點效果2、for (int i = 0; i < tabWidget.getChildCount(); i++) {
//擷取tabs圖片;
ImageView iv=(ImageView)tabWidget.getChildAt(i).findViewById(android.R.id.icon);
iv.setLayoutParams(new LayoutParams(width, height));
}
但這種情況的話,程式會出錯,這是為什麼呢,因為在new LayoutParams(width, height)的時候一定要用父控制項的LayoutParams,而我們在導包的時候並沒有發現有適合的在組織匯入的時候,所有的結果發現無論到那個都會出錯,3、所以我們必須尋求另外一種方法 for(int i=0;i < tabs.getChildCount();i++)
{
TextView textView = (TextView)tabs.getChildAt(i).findViewById(android.R.id.title);
textView.setTextSize(14);
textView.setPadding(0, 3, 0, 0);
ImageView image = (ImageView)tabs.getChildAt(i).findViewById(android.R.id.icon);
image.getLayoutParams().height = 30;//通過給它的屬性賦值的方法可以解決問題
image.getLayoutParams().width = 30;
}結果完成最初設定的目標