2011-10-16 54 views
1

我使用Tab鍵的佈局和我想要做的兩件事情:安卓:文本和彩色標籤上,標籤佈局

  1. 設定的顏色,所以它不會是灰色
  2. 減少文字大小,文字不適合。

此外,文本大部分是在圖標上,而不是在下面(我可以做些什麼嗎?)。

任何想法,我該如何做到這一點?

編輯:我以這種方式創建一個新的標籤:

spec = tabHost.newTabSpec("artists").setIndicator(
    "Artists", 
    res.getDrawable(R.drawable.ic_tab_artists) 
).setContent(intent); 
tabHost.addTab(spec); 

我想改變「藝術家」字的大小。

回答

5

你應該定義自己的看法。

tabHost.newTabSpec("tab1") 
       .setIndicator(prepareTabView(this, "title")) 
       .setContent(intent); 

,你可以改變文字大小這裏tv.setTextSize(20) 「

public static View prepareTabView(Context context, String text) { 
     View view = LayoutInflater.from(context).inflate(
       R.layout.tab_indicator, null); 
     TextView tv = (TextView) view.findViewById(R.id.tabIndicatorTextView); 
     tv.setText(text); 

     return view; 
    } 

tab_indicator.xml你可以改變這裏的文字大小也機器人:TEXTSIZE =」 20dip 「,可以在這裏設置背景顏色的android:背景=」 @顏色/ back_color_selector_tab」

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fakeNativeTabLayout" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:gravity="center" 
    android:orientation="vertical" android:background="@color/back_color_selector_tab"> 
    <!-- You can even define an Icon here (dont forget to set a custom icon 
     in your code for each Tab): <ImageView android:id="@+id/fakeNativeTabImageView" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:src="@drawable/icon" /> --> 
    <TextView android:id="@+id/tabIndicatorTextView" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:text="Tab" android:ellipsize="marquee" /> 

</LinearLayout> 

back_color_selector_tab.xml是用於在不同狀態下自動更改背景顏色的xml。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="@drawable/state_orange" /> 
    <item android:state_selected="true" android:drawable="@drawable/background05" /> <!-- focused --> 
    <item android:drawable="@drawable/background04" /> <!-- default --> 
</selector> 

state_orange.xml樣本

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid android:color="@color/orange" /> 
</shape> 
+1

你能解釋一下代碼的每個部分是什麼嗎? 我不明白它是如何做A和B ... – Belgi

+0

在這個示例中,每件事都在你的手中,並取決於tab_indicator.xml文件的設計 – breceivemail

+0

我嘗試使用該代碼,並且我有一些問題:A. on prepareTabView它不識別R.layout.tab_indicator B.can我選擇顏色與十六進制三元組(如#007FFF)? C. 是一個錯誤... – Belgi

0

對於選項A:

for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) 
    { 
     tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#8A4117")); 
    } 
     tabHost.getTabWidget().setCurrentTab(1); 
     tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#C35817")); 
+0

能否請您解釋一下後兩行呢? 爲什麼不是tabHost.getTabWidget().setCurrentTab(0),並且最後一行是否僅爲特定選項卡更改背景顏色? – Belgi

+0

它用於突出顯示不同顏色的當前標籤背景。然後實現OnTabChangeListener,這樣你就可以在不同的背景中突出顯示選定的選項卡,然後取消選中的選項卡 –

+0

謝謝,是否也可以改變選項卡下方該行的顏色? – Belgi