控制在TabActivity标签颜色状态/大小? [英] Controlling Tab colour-state / size in a TabActivity?

查看:150
本文介绍了控制在TabActivity标签颜色状态/大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,这是推动我坚果 - 我已经找遍了所有的引用和例子我能找到的,我似乎仍然失去了一些东西真的很明显。这些是标签为7天电视指南(通常不与红色箭头,显然:))...

Ok, this is driving me nuts - I've searched all of the references and examples I can find and I still seem to be missing something really obvious. These are the tabs for a 7-day TV Guide (not normally with the red arrow, obviously :) )...

我需要知道的是什么是对象(查看或绘制对象我假设),构成了一个标签本身的主体/背景是什么? (如红色箭头所示)以及如何访问它还是有它自动的状态颜色更改为我选择的名单?另外,我怎么能得到指标的TextView的状态颜色跟风?

What I need to know is what is the object (View or Drawable I assume) that makes up the main body/background of a Tab itself? (as indicated by the red arrow) and how do I access it or have it automatically change its state colour to a list of my choice? Also, how can I get the state colour of the indicator TextView to follow suit?

例:在上面的捕捉,这是可读的,因为我已经设置的文字颜色为静态灰色(而不是明亮的白色它消失选定的选项卡上)。但我想它会自动成为白色标签(选择)和黑色亮白色文本(选择)黑色文本。

Example: In the capture above, it's readable because I've set the textColor to a static grey (instead of the bright white which disappeared on a selected tab). But I want it to automatically become black text on white tab (selected) and bright white text on black (for unselected).

所有帮助感激地接受。

推荐答案

这将重新presents每个标签可以通过改变视图

The view which represents each tab can be changed using

setIndicator(View)

我一直在使用这种code到打造每个选项卡:

I've been using this code to build each tab :

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

这里来布局/ tab.xml

And here comes 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的对文字颜色选择器(改变文字颜色时选定/ pressed等)。这种方式可以使文字看起来黑色的标签时是pressed,白当它不:)

Note that LinearLayout has a selector on it's background (to change backround, obviously :) ), and the TextView has a selector on the textColor (to change the text color when selected / pressed etc.). It this way you can make the text look black when tab is pressed, and white when it is not :)

这篇关于控制在TabActivity标签颜色状态/大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆