1

私は Android アプリケーションを作成しており、xml にタブホストと 2 つのタブがあります。私は彼らに電話しましたが、彼らはうまく機能しています。しかし、タブの醜いグレー/オレンジ色を取り除きたいです。画像を使って背景を設定してみました。私はこのコードを使用しました:

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

しかし、次のように表示されます。

間違った背景

グレーとオレンジの色を置き換えるにはどうすればよいですか?

ありがとう

4

2 に答える 2

1

tabwidget を完全にカスタマイズする手順:

  1. tabwidget のカスタム レイアウトを作成します。このレイアウトは、 oneIconと oneで構成されるデフォルトのレイアウトに似ていますTextView。お好きなレイアウトをお選びいただけます。親レイアウトにはステートフルな background があることに注意してください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。タブウィジェット用に描画可能なテキスト、アイコンを設定します

    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);
于 2012-11-20T09:43:52.820 に答える
0

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>

于 2012-11-20T09:23:48.460 に答える