如何在TabHost中更改选项卡的颜色(

时间:2017-03-02 11:43:14

标签: android android-tabhost

默认背景为黑色,我希望它有不同的颜色。我认为答案在下面的XML中(但我可能是错的)。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"

    >
    <!-- When selected -->
    <item android:drawable="@drawable/login"
          android:state_selected="true"


        />
    <!-- When not selected -->
    <item android:drawable="@drawable/login"

        />
</selector>

这是Mainactivity

TabHost tabHost = getTabHost();


    // Tab for login
    TabSpec Login = tabHost.newTabSpec("Login");
    // setting Title and Icon for the Tab

    Login.setIndicator("", getApplicationContext().getResources().getDrawable(R.drawable.drawtab1));
    Intent LoginIntent = new Intent(this, Tab1Activity.class);
    LoginIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    Login.setContent(LoginIntent);

2 个答案:

答案 0 :(得分:1)

嗯,你可以像这样以编程方式改变它。

tabHost.getTabWidget().getChildAt(index).setBackgroundColor(Color.parseColor("#93BEDF"));

答案 1 :(得分:0)

我的XML代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.jmtechnologies.balajitravels.car.CarBooking">

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="55dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@color/newcolor">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="Hotel Booking"
            android:textColor="@color/white"
            android:textSize="22sp"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/imageView111"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="15dp"
            android:background="@drawable/backblack_white" />

    </RelativeLayout>


    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/relativeLayout1">

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

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/newcolor"
                    android:orientation="horizontal" />

                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_weight="0" />

                <android.support.v4.view.ViewPager
                    android:id="@+id/viewpager"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom" />
            </LinearLayout>
        </TabHost>
    </RelativeLayout>

</RelativeLayout>

活动代码

package com.jmtechnologies.balajitravels.Hotel;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.jmtechnologies.balajitravels.MyTabFactory;
import com.jmtechnologies.balajitravels.R;

public class HotelBooking extends FragmentActivity implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {

    private HotelPagerAdapter mAdapter;
    private ViewPager mViewPager;
    private TabHost mTabHost;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.hotel_booking);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);

        mViewPager = (ViewPager) findViewById(R.id.viewpager);

        // Tab Initialization
        initialiseTabHost();
        mAdapter = new HotelPagerAdapter(getSupportFragmentManager());
        // Fragments and ViewPager Initialization


        mViewPager.setAdapter(mAdapter);
        mViewPager.setOnPageChangeListener(HotelBooking.this);


            /*GridView listview = (GridView)findViewById(R.id.gridView1);
            ProductGrid context = this;
            listview.setAdapter(new GridAdapter(this, tab, title,price));*/

        ImageView back = (ImageView) findViewById(R.id.imageView111);

        back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }
        });
    }

    private static void AddTab(HotelBooking activity, TabHost tabHost, TabHost.TabSpec tabSpec) {
        tabSpec.setContent(new MyTabFactory(activity));
        tabHost.addTab(tabSpec);
    }

    // Manages the Tab changes, synchronizing it with Pages
    public void onTabChanged(String tag) {
        int pos = this.mTabHost.getCurrentTab();
        this.mViewPager.setCurrentItem(pos);
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
    }

    // Manages the Page changes, synchronizing it with Tabs
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        int pos = this.mViewPager.getCurrentItem();
        this.mTabHost.setCurrentTab(pos);
    }

    @Override
    public void onPageSelected(int arg0) {
    }


    // Tabs Creation
    private void initialiseTabHost() {
        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();

        // TODO Put here your Tabs
        HotelBooking.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("Domestic").setIndicator("Domestic"));
        HotelBooking.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("International").setIndicator("International"));
        for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
            // mTabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); // unselected
            TextView tv = (TextView) mTabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
            tv.setTextColor(Color.parseColor("#ffffff"));
        }


        mTabHost.setOnTabChangedListener(this);
    }

}