在TabActivity中控制选项卡颜色状态/大小?

Squ*_*onk 5 android tabwidget tabactivity

好吧,这让我疯了 - 我搜索了所有可以找到的参考文献和例子,我似乎仍然遗漏了一些非常明显的东西.这些是7天电视指南的标签(通常不带红色箭头,显然:) :)

在此输入图像描述

我需要知道的是构成Tab本身主体/背景的对象(我认为是View还是Drawable)是什么?(如红色箭头所示)以及如何访问它或让它自动将其状态颜色更改为我选择的列表?另外,如何让指标TextView的状态颜色跟随呢?

示例:在上面的捕获中,它是可读的,因为我已将textColor设置为静态灰色(而不是在所选选项卡上消失的亮白色).但我想让它自动变为白色标签上的黑色文字(选中)和黑色的明亮白色文字(未选中).

感谢所有的帮助.

Koc*_*cus 6

可以使用更改代表每个选项卡的视图

setIndicator(View)
Run Code Online (Sandbox Code Playgroud)

我一直在使用此代码来构建每个选项卡:

View view = buildTabView(this, "Friday");
TabHost.TabSpec spec = tabHost.newTabSpec("cat1").setIndicator(view).setContent(intent);
tabHost.addTab(spec);

public static LinearLayout buildTabView(Context context, String label){
    LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);          
    final LinearLayout ll = (LinearLayout)li.inflate(R.layout.tab, null);

    // the following lines will change the tabs size depending on the label (text) length.
    // the longer tab text - the wider tabs
    LinearLayout.LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, label.length() + 1);
    ll.setLayoutParams(layoutParams);

    final TextView tv = (TextView)ll.findViewById(R.id.tab_tv);         
    tv.setOnTouchListener(new OnTouchListener() {               
        public boolean onTouch(View v, MotionEvent event) {
            ll.onTouchEvent(event);
            return false;
        }
    });

    tv.setText(label);          
    return ll;
}
Run Code Online (Sandbox Code Playgroud)

这里有layout/tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/tab_bg_selector"
  android:clickable="true"
  >

  <TextView
  android:id="@+id/tab_tv"
  android:layout_width="wrap_content"
  android:layout_height="33dip"
  android:text="Text 1"
  android:textStyle="bold"
  android:textSize="16dip"
  android:gravity="center"
  android:textColor="@drawable/tab_color_selector"
  android:layout_weight="1.0"
  android:clickable="true"
  />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

请注意,LinearLayout在其背景上有一个选择器(改变背景,显然是:)),TextView在textColor上有一个选择器(在选择/按下时更改文本颜色等).通过这种方式,您可以在按下选项卡时使文本看起来是黑色,而当它不是时,可以使白色看起来是:)