好的,这让我疯了 - 我搜索了所有可以找到的参考文献和例子,我似乎仍然遗漏了一些非常明显的东西。这些是7天电视指南的标签(通常不带红色箭头,显然:) :) ...
我需要知道的是构成Tab本身主体/背景的对象(我认为是View还是Drawable)是什么? (如红色箭头所示)以及如何访问它或让它自动将其状态颜色更改为我选择的列表?另外,如何让指标TextView的状态颜色跟随呢?
示例:在上面的捕获中,它是可读的,因为我将textColor设置为静态灰色(而不是在选定的选项卡上消失的亮白色)。但是我想让它自动变成白色标签上的黑色文字(选中)和黑色的明亮白色文字(未选中)。
感谢所有的帮助。
答案 0 :(得分: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上有一个选择器(在选择/按下时更改文本颜色等)。通过这种方式,您可以在按下选项卡时使文本看起来是黑色,而当不是时,可以使白色看起来是白色的。)
答案 1 :(得分:2)
请使用您使用的代码更新您的问题..您是否在drawable中使用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-widget。
<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布局。