5

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

在此处输入图像描述

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

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

感谢所有帮助。

4

2 回答 2

6

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

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 上有一个选择器(在选择/按下时更改文本颜色等)。这样,您可以在按下制表符时使文本看起来是黑色的,而不是在按下时显示为白色:)

于 2011-04-14T23:43:12.530 回答
2

请用您使用的代码更新您的问题。您是否在可绘制对象中使用 xml 来为选项卡设置动画?这是使用 xml 处理的示例选项卡操作。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/ic_tab_about_grey"
          android:state_selected="false" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/ic_tab_about_color" android:state_selected="true"/>
</selector>

使用此 xml 文件自定义选项卡行为和图标。

这是在选项卡中设置此动画/自定义选项的代码:

intent = new Intent().setClass(this, sms.class);
spec = tabHost.newTabSpec("sms").setIndicator("SMS",
       res.getDrawable(R.drawable.ic_tab_sms))
       .setContent(intent);
tabHost.addTab(spec);

现在布局中的 XML 来定义 tab-host 和 tab-widgets。

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
</TabHost>

使用您自己的颜色、字体和结构自定义此 xml 布局。

于 2011-04-18T07:03:16.343 回答