簡體   English   中英

在Eclipse中設置選項卡背景

[英]Setting tab background in eclipse

我正在制作一個android應用程序,我在XML中有一個tabhost和兩個標簽。 我打電話給他們,他們工作得很好。 但我想擺脫選項卡上的難看的灰色/橙色。 我嘗試使用圖像設置背景。 我使用以下代碼:

th.getTabWidget().setBackgroundResource(R.drawable.tab_normal);

但它顯示如下:

錯誤的BG

我如何獲得它來代替灰色和橙色?

謝謝

完全自定義tabwidget的步驟:

  1. 為tabwidget創建自定義布局:此布局類似於默認布局,該布局由一個Icon和一個TextView 您可以選擇所需的任何布局。 注意,父布局具有有狀態的背景tabwidget_selector 此可繪制對象是替換默認橙色焦點顏色的關鍵。 您可以選擇自己的焦點顏色。

     <?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="40dp" android:background="@drawable/tabwidget_selector" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/tabIndicatorIcon" android:layout_width="28dp" android:layout_height="28dp" android:layout_marginTop="2dp" /> <TextView android:id="@+id/tabIndicatorText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="2dp" android:textColor="@color/white" /> 

  2. 寫函數返回tabwidget的視圖。 設置文字,可為您的Tabwidget繪制圖標

     private View getTabIndicatorView(Context context, String tag, int drawable) { View view = LayoutInflater.from(context).inflate( R.layout.tab_widget_custom, null); TextView tv = (TextView) view.findViewById(R.id.tabIndicatorText); tv.setText(tag); ImageView tabIndicatorIcon = (ImageView) view .findViewById(R.id.tabIndicatorIcon); tabIndicatorIcon.setBackgroundResource(drawable); return view; } 
  3. 在您的活動中使用此功能:

TabHost.TabSpec setIndicator(View view)指定一個視圖作為選項卡指示器。

       Intent intent;
        TabHost.TabSpec spec;

        spec = tabHost.newTabSpec("Inbox").setIndicator(
                getTabIndicatorView(MainTabActivity.this, "Hộp thư",
                        R.drawable.tin_nhan_tab_selector));
        intent = new Intent(this, xxxx.InboxActivity.class);

        spec.setContent(intent);
        tabHost.addTab(spec);

您可以為tabwidget設置背景:

   <TabHost
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<RelativeLayout
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">

        <TabWidget
        android:id="@android:id/tabs"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/tabbackground"
        android:layout_margin="7dp"

        />


        <FrameLayout
            android:id="@android:id/tabcontent" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent" 
            android:layout_above="@android:id/tabs" 
            android:layout_alignParentLeft="true" 
            android:layout_alignParentTop="true" 
            android:layout_margin="5dp" 
            android:background="@drawable/list_shape">
         </FrameLayout>
</RelativeLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM