2017-03-02 96 views
0

默认背景是黑色的,我想它不同的颜色。我想答案是下面的XML内(但我可能是错的)。如何改变标签的颜色TabHost(

<?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); 

回答

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); 
    } 

} 
1

那么你可以通过编程改变它这个样子。

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

它为你的先生工作? – Remario

+0

谢谢。对不起,它没有 - 确定哪个索引是。 –

+0

您试图修改的孩子的索引。例如第一个孩子索引= 0 – Remario

0

变化state_selected到state_enabled 和定制tabhost背景 使用这个选择

<?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_enabled="true"/> 
</selector> 

和改变选择的标签颜色,你必须处理它编程

fragmentTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() { 
     @Override 
     public void onTabChanged(String s) { 
      //this change previous selected tab color to default color 
      for (int i = 0; i < fragmentTabHost.getTabWidget().getChildCount(); i++) 
       fragmentTabHost.getTabWidget().getChildAt(i).setBackgroundColor(ContextCompat.getColor(getContext(), R.color.backgroundColor)); 
      //this line change selected tab color 
      fragmentTabHost.getTabWidget().getChildAt(fragmentTabHost.getCurrentTab()).setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorAccent)); 
     } 
    }); 
+0

你能澄清你的答案吗? –