添加图像或者样式每个标签 [英] Add Image or style to each tab

查看:128
本文介绍了添加图像或者样式每个标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多标签的应用程序,我想添加图片或样式每个选项卡,怎么请?

i have app with many tabs, i want to add image or style to each tab , how please?

th = (TabHost) findViewById(R.id.tabhost_template_two_tabs);
th.setup();
// all tab
spec = th.newTabSpec("All");
spec.setIndicator("All");
spec.setContent(R.id.tab_template_two_tabs_all);
th.addTab(spec);
// favorite tab
spec = th.newTabSpec("Favorite");
spec.setIndicator("Favorite");
spec.setContent(R.id.tab_template_two_tabs_favorite);
th.addTab(spec);
th.setCurrentTab(1);

感谢

推荐答案

试试这个: 从你的OnCreate()方法调用这样的:

Try this : Call this from your OnCreate() method:

 setTabs();

然后把这个code

Then put this code

 private void setTabs()
{
    addTab(R.drawable.ic_icon1, Activity.class, "All");
    addTab(R.drawable.ic_icon2, Activity1.class, "Favorite");
}

private void addTab(int drawableId, Class<?> c, String labelId)
{
    final TabHost tabHost = getTabHost();
    Intent intent = new Intent(this, c);
    TabHost.TabSpec spec = tabHost.newTabSpec("tab"+ labelId);  

    View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
    TextView title = (TextView) tabIndicator.findViewById(R.id.title);
    title.setText(labelId);

    ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
    icon.setImageResource(drawableId);
    spec.setIndicator(tabIndicator);
    spec.setContent(intent);
    tabHost.addTab(spec);

}

然后创建tab_indicator.xml在绘制文件夹,并把这个code.There您可以设置不同的颜色,当它是pressed,集中等等...

Then Create tab_indicator.xml in drawable folder and put this code.There you can set different colours when it is pressed,focused etc...

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false"
    android:state_pressed="false" android:drawable="@drawable/tab_unselected" />
<item android:state_focused="false" android:state_selected="true"
    android:state_pressed="false" android:drawable="@drawable/tab_selected" />

<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false"
    android:state_pressed="false" android:drawable="@drawable/tab_focus" />
<item android:state_focused="true" android:state_selected="true"
    android:state_pressed="false" android:drawable="@drawable/tab_focus" />

<!-- Pressed -->
<item android:state_selected="true" android:state_pressed="true"
    android:drawable="@drawable/tab_focus" />
<item android:state_pressed="true" android:drawable="@drawable/tab_press" />
  </selector>

在布局中创建tab_indicator.xml,并把这个code:

In layouts create tab_indicator.xml and put this code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="55dip"    
android:layout_weight="1"
android:orientation="vertical"

android:background="@drawable/tab_indicator"
android:padding="5dp">

<ImageView android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:src="@drawable/icon"

/> 

<TextView android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true"
    style="?android:attr/tabWidgetStyle"
/>    
     </RelativeLayout>

这篇关于添加图像或者样式每个标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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