2012-08-16 19 views
4

我是android.I的新手,我正在嘗試實現一個具有三個選項卡的動作條,每個選項卡包含一個圖標和tab.tab的名稱,我成功地放置了圖標和每個選項卡上的文本,但不幸的是,圖標出現在選項卡中文本的左側(選項卡的名稱)。我想在文本的頂部放置圖標而不是左側。請查找我的代碼片段,並請幫助我找到解決方案。在此先感謝,如何在android中的動作條文本的頂部添加圖標

 private void setActionBar() 

     {    

      ActionBar bar = getActionBar(); 

    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
    bar.setDisplayShowHomeEnabled(false); 
    bar.setDisplayShowTitleEnabled(false); 

    ActionBar.Tab tabA = bar.newTab().setText("TabA"); 
     tabA.setIcon(R.drawable.iconA); 

      ActionBar.Tab tabB = bar.newTab().setText("TabB"); 
    tabB.setIcon(R.drawable.iconB); 

    ActionBar.Tab tabC = bar.newTab().setText("TabC"); 
      tabC.setIcon(R.drawable.iconC); 
     } 

回答

5

您可以使用自定義視圖來定義如何顯示標籤。

  1. 定義,你在你的活動將文本
  2. 以上的圖像,膨脹的觀點,併爲您的形象和價值的文字
  3. 組自定義視圖選項卡
自定義佈局

這裏是一個粗略的例子:

自定義佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_horizontal" 
    android:orientation="vertical" > 

<ImageView 
    android:id="@+id/tabIcon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:paddingTop="2dp" /> 

<TextView 
    android:id="@+id/tabText" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:textColor="#FFFFFF" /> 

</LinearLayout> 

充氣查看該定標籤

Tab tab = actionBar.newTab().setCustomView(tabView) 
+1

View tabView = activity.getLayoutInflater().inflate(R.layout.actiobar_tab, null); TextView tabText = (TextView) tabView.findViewById(R.id.tabText); tabText.setText(R.String.sometext); ImageView tabImage = (ImageView) tabView.findViewById(R.id.tabIcon); tabImage.setImageDrawable(activity.getResources().getDrawable(R.drawable.someimage)); 

設置自定義視圖我是不是能夠膨脹的觀點在SectionsPagerAdapter,但事實證明,你不需要它充氣。只是'Tab tab = actionBar.newTab()。setCustomView(R.layout.yourView)'。 – Noumenon 2013-09-18 01:59:57

相關問題