我有2個關(guān)于tabHost的問題:
我用2個標(biāo)簽創(chuàng)建了tabHost
對于標(biāo)簽標(biāo)題,我使用setIndicator(TextView)
(我使用api 4級)
我的標(biāo)題背景是白色的.我使用選擇器作為標(biāo)題在標(biāo)題的差異圖像之間進(jìn)行選擇.
>我想僅在選擇/按下時使標(biāo)題文本變?yōu)榇煮w.我沒有成功使用我擁有的選擇器.我能做到嗎?我的想法是,在我使用drawable a的情況下,我想要文本粗體.其他案件不大膽.關(guān)于textColor的同樣問題.
>它看起來像一個錯誤 – 當(dāng)標(biāo)簽首次打開時,所選標(biāo)簽上的文本(我在tabHost.setCurrentTab(tabId)中使用的文本)根本看不到.在第一次按下/聚焦/聚焦任何其他項目后,它看起來很好.任何想法為什么或如何解決這個問題?
提前致謝
on tabActivity –
TextView title1 = new TextView(MainActivity.getInstnace(), null, android.graphics.Typeface.NORMAL);TextView title2 = new TextView(MainActivity.getInstnace(), null, android.graphics.Typeface.NORMAL);title1.setText("teb11 title");title1.setBackgroundResource(R.drawable.tabtitle);title1.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.tab1), null, null, null);title2.setText("tab22 title");title2.setBackgroundResource(R.drawable.tabtitle);title2.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.tab2), null, null, null);TabSpec tab1 = mTabHost.newTabSpec("tab1").setIndicator(title1).setContent(R.id.list1);TabSpec tab2 = mTabHost.newTabSpec("tab2").setIndicator(title2).setContent(R.id.list2);mTabHost.addTab(tab1);mTabHost.addTab(tab2); mTabHost.setCurrentTab(0);
選擇器tab1.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@drawable/iconselect"/> <item android:state_pressed="true" android:drawable="@drawable/iconselect"/> <item android:drawable="@drawable/icon"/> </selector>
tabTitle的選擇器
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/tabselected"/> <item android:state_selected="true" android:drawable="@drawable/tab" /> <item android:state_focused="true" android:drawable="@drawable/tab" /> </selector>
解決方法:
對于你的問題#1:
在你的TabsAdapter上使用onPageSelected(int position),如下所示:
public void onPageSelected(int position) { //your logic here fixTitleText(); }
和:
private void fixTitleText() { for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i ) { View view = mTabHost.getTabWidget().getChildAt(i); TextView tv = (TextView) view.findViewById(android.R.id.title); tv.setTextColor(getResources().getColor(R.drawable.text_selector)); tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14); tv.setTypeface(null, Typeface.BOLD); } }
請注意,此代碼適用于HONEYCOMB及更高版本,在此之前,選項卡主機(jī)視圖層次結(jié)構(gòu)略有不同
來源:https://www.icode9.com/content-4-314351.html