android - 在 TabActivity 中控制 Tab 颜色状态/大小?

标签 android tabwidget tabactivity

好吧,这让我发疯了——我已经搜索了所有我能找到的引用资料和示例,但我似乎仍然遗漏了一些非常明显的东西。这些是 7 天电视指南的选项卡(通常没有红色箭头,显然 :) )...

enter image description here

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

示例:在上面的捕获中,它是可读的,因为我将 textColor 设置为静态灰色(而不是在选定选项卡上消失的亮白色)。但我希望它自动变成白色选项卡上的黑色文本(选中)和黑色亮白文本(未选中)。

感谢所有帮助。

最佳答案

代表每个选项卡的 View 可以使用

setIndicator(View)

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

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

这里是 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>

请注意,LinearLayout 在它的背景上有一个选择器(显然是为了改变背景 :)),而 TextView 在 textColor 上有一个选择器(在选择/按下时改变文本颜色等)。通过这种方式,您可以在按下 tab 时使文本看起来是黑色的,而在未按下 tab 时则使文本看起来是白色的:)

关于android - 在 TabActivity 中控制 Tab 颜色状态/大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5407300/

相关文章:

android - 开源文件转换为可闪存 .img ??如何?

android - 在android中的布局之间切换

android - 识别 Android 平板电脑是否有 sim 卡插槽

android - (Android) 如何更改 tabWidget 中选项卡的大小?

Android ActionBar 后退按钮和选项卡

android - 从后台进程更新 TabActivity 的子 Activity 的 View

android - 清除 WebView 创建的所有缓存项?

android - 如何删除android中tabbar的默认 View

安卓。如何更改选项卡内的 Activity

android - 警告从 TabActivity 中的 ActivityGroup 打开新 Activity