2011-10-27 109 views
1

[底部的解決方案]標籤圖標來改變點擊

我需要一個標籤有以下behaveyour但我不知道如何實現它:

  • 的標籤需要處於最底層。
  • 標籤需要圖標&文字。
  • 每個圖標需要不同。
  • 圖標需要改變顏色時選擇和選項卡需要覆蓋。

我這麼模糊地問這個問題的原因是,我嘗試過一種方法,但碰到了一堵磚牆,所以我想知道更有經驗的人會怎麼做。

作爲請求源的一些: CustomTabActivity.java

private TabHost mTabHost; 
private Context ctx = this; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // construct the tabhost 
    setContentView(R.layout.main); 

    setupTabHost(); 
    mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider); 
    setupTab(new TextView(this), "t1", R.drawable.myimg1); 
    setupTab(new TextView(this), "t2", R.drawable.myimg2); 
    mTabHost.setOnTabChangedListener(new OnTabChangeListener(){ 

     @Override 
     public void onTabChanged(String tabId) { 
      Log.e("Simon", "Current tab - " + Integer.toString(mTabHost.getCurrentTab())); 
      View currentView = mTabHost.getChildAt(mTabHost.getCurrentTab()); 
      currentView.getClass(); 
     } 

    }); 
} 

private void setupTabHost() { 
    mTabHost = (TabHost) findViewById(android.R.id.tabhost); 
    mTabHost.setup(); 
} 

private void setupTab(final View view, final String tag, int num) { 
    View tabview = createTabView(mTabHost.getContext(), tag, num); 

    TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() { 
     public View createTabContent(String tag) {return view;} 
    }); 
    mTabHost.addTab(setContent); 
} 

private static View createTabView(final Context context, final String text, final int num) { 
    View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null); 
    TextView tv = (TextView) view.findViewById(R.id.tabsText); 
    tv.setText(text); 

    ImageView iv = (ImageView) view.findViewById(R.id.tabsImg); 

    iv.setImageResource(num); 
    return view; 
} 

main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> 
     <LinearLayout android:orientation="vertical" 
      android:layout_width="fill_parent" android:layout_height="fill_parent"> 
      <FrameLayout android:id="@android:id/tabcontent" 
       android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> 
       <ViewStub android:id="@+id/stub1" 
        android:inflatedId="@+id/subTree" 
        android:layout="@layout/mylayout" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" /> 
      </FrameLayout> 
      <TabWidget android:id="@android:id/tabs" 
       android:layout_width="fill_parent" android:background="@drawable/tabs_bg" android:layout_height="wrap_content"/> 
     </LinearLayout> 
    </TabHost> 

</LinearLayout> 

mylayout.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
    <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" style="?android:attr/progressBarStyleLarge" android:id="@+id/progressBar1"></ProgressBar> 

</LinearLayout> 

--------- -----解決方案-------------

所以問題就解決了。我已經從res/drawable- [hdpi | ldpi | mdpi]文件夾中將靜態圖像替換爲R.drawable.myimg1,並將其替換爲res/drawable中的新xml文件,如下所示:

mydrawable1.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true" 
     android:drawable="@drawable/myimg1_selected"/> 
    <item android:drawable="@drawable/myimg1"/> 
</selector> 

其中myimg1和myimg1_selected是我希望以不同狀態顯示的圖像。

+0

哪部分不工作? –

+0

嗯,我有底部的標籤,覆蓋和不同的圖標。我無法獲取,因此圖標顏色發生變化。 – OrhanC1

+1

你能發表一些代碼嗎? –

回答

1

檢查this tutorial。它介紹如何選擇標籤時如何更改圖標。

+0

我只是在通過測試項目中的某些東西工作。如果它有效,你已經得到了答案:) – OrhanC1

+0

得到了解決方案。現在編輯我的問題。 – OrhanC1