AlertDialog中的TabHost

问题描述:

使用this answer,我设法在AlertDialog中获得一个TabHost。AlertDialog中的TabHost

的问题是,跳指示器具有的颜色与背景相同的,如下面的图像中:

Tab Indicator text color is the same as the background

用于显示的对话框的代码如下呈现:

 AlertDialog.Builder builder; 
     AlertDialog alertDialog; 

     LayoutInflater inflater = (LayoutInflater) 
       getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE); 
     View layout = inflater.inflate(R.layout.tabbed_dialog, 
       (ViewGroup) findViewById(R.id.tabhost)); 

     TabHost tabs = (TabHost) layout.findViewById(R.id.tabhost); 
     tabs.setup(); 
     TabHost.TabSpec tabpage1 = tabs.newTabSpec("foo"); 
     tabpage1.setContent(R.id.ScrollView01); 
     tabpage1.setIndicator("foo"); 
     TabHost.TabSpec tabpage2 = tabs.newTabSpec("bar"); 
     tabpage2.setContent(R.id.ScrollView02); 
     tabpage2.setIndicator("bar"); 
     tabs.addTab(tabpage1); 
     tabs.addTab(tabpage2); 

     builder = new AlertDialog.Builder(MainActivity.this); 

     builder 
       .setCancelable(false) 
       .setPositiveButton("Ok", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 

          } 
         }) 
       .setNegativeButton("Cancel", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           dialog.cancel(); 
          } 
         }); 
     builder.setTitle("Dialog with tabs"); 
     builder.setView(layout); 
     alertDialog = builder.create(); 
     alertDialog.show(); 

这是所使用的布局:

<?xml version="1.0" encoding="utf-8"?> 
<TabHost 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

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

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"/> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:padding="5dp"> 

      <ScrollView android:id="@+id/ScrollView01" 
       android:layout_width="wrap_content" 
       android:layout_height="200px"> 

       <TextView 
        android:id="@+id/TextView01" 
        android:text="foo text content" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="center_horizontal" 
        android:paddingLeft="15dip" 
        android:paddingTop="15dip" 
        android:paddingRight="20dip" 
        android:paddingBottom="15dip" 
        android:textColor="#000000"/> 

      </ScrollView> 


      <ScrollView android:id="@+id/ScrollView02" 
       android:layout_width="wrap_content" 
       android:layout_height="200px"> 

       <TextView 
        android:id="@+id/TextView02" 
        android:text="bar text content" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="center_horizontal" 
        android:paddingLeft="15dip" 
        android:paddingTop="15dip" 
        android:paddingRight="20dip" 
        android:paddingBottom="15dip" 
        android:textColor="#000000"/> 

      </ScrollView> 
     </FrameLayout> 
    </LinearLayout> 
</TabHost> 

问题是:如何更改指示器颜色?

我知道我可以改变由AlertDialog使用的布局,如:

builder = new AlertDialog.Builder(MainActivity.this, android.R.style.Theme_Dialog); 

但我想要的布局展现在画面相同,但不同颜色的标签指标。我可以从Android获得这个,而不必重新定义整个指标吗?

找到解决方案。
刚拿到冠军查看和设置颜色:

 TextView tv = (TextView)layout.findViewById(android.R.id.title); 
     tv.setTextColor(Color.GRAY); 

,或者甚至更好,我们可以选择的每个选项卡:

 TextView tv; 
     tv = (TextView)tabs.getTabWidget().getChildAt(0).findViewById(android.R.id.title); 
     tv.setTextColor(Color.GRAY); 
     tv = (TextView)tabs.getTabWidget().getChildAt(1).findViewById(android.R.id.title); 
     tv.setTextColor(Color.GRAY);