2012-09-19 24 views
0

我已創建在機器人分頁唯一的圖像(不包含任何文本),我想在選項卡。還有的中心將被顯示的圖像是多個標籤,每個標籤包含圖像,其顯示,但問題是該圖像不顯示在每個選項卡的中心。如何在標籤中心顯示圖像?

,這裏是我的xml

<RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_above="@android:id/tabs" 
      /> 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:background="#000000" > 
     </TabWidget> 
    </RelativeLayout> 

</TabHost> 

在代碼中設置圖像:

newsTab.setIndicator("",getResources().getDrawable(R.drawable.news)).setContent(
       new Intent(RestauActivity.this, NewsActivity.class)); 

回答

4

1) - 創建名爲report_tabs.xml或者你喜歡的任何名稱的佈局文件。

在report_tabs使用此代碼。

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout_tabsLayout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/btn_selector" 
    android:gravity="center" 
    android:orientation="vertical" 
    > 

    <ImageView 
    android:id="@+id/img_icon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_marginTop="3dp" 
    /> 
    </LinearLayout> 

2) - 和您的活動使用下面的代碼。

Intent intent; 
    tabhost = getTabHost(); 
    TabHost.TabSpec tabspec; 
    intent = new Intent().setClass(getApplicationContext(),xxxxx.class); 

    tabspec = tabhost.newTabSpec("First"); 
    view = LayoutInflater.from(this).inflate(R.layout.report_tabs, 
tabhost.getTabWidget(), false); 

    imgtabF = (ImageView) view.findViewById(R.id.img_icon); 
    imgtabF.setBackgroundResource(R.drawable.tab_icon_selector); 

    tabspec.setIndicator(view); 
    tabspec.setContent(intent); 
    tabhost.addTab(tabspec); 

3) - 創建繪製與名tab_icon_selector文件改變在選項卡中單擊該圖標是這樣的: -

<?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- Non focused states --> 
    <item android:state_focused="false" android:state_selected="false" 
      android:state_pressed="false" android:drawable="@drawable/medical_icon_unselect"  
    /> 
    <item android:state_focused="false" android:state_selected="true" 
    android:state_pressed="false" android:drawable="@drawable/medical_icon_sel" /> 
    <!-- Focused states --> 
    <item android:state_focused="true" android:state_selected="false" 
    android:state_pressed="false" android:drawable="@drawable/medical_icon_sel" /> 
    <item android:state_focused="true" android:state_selected="true" 
    android:state_pressed="false" android:drawable="@drawable/medical_icon_sel" /> 
    <!-- Pressed --> 
    <item android:state_pressed="true" android:drawable="@android:color/transparent"/> 
    </selector> 

現在,您可以創建自定義標籤欄和你的形象圖標會在標籤的中心。

+0

感謝您的回覆 – vaibvorld