【问题标题】:Add Image or style to each tab为每个选项卡添加图像或样式
【发布时间】:2013-01-26 08:07:04
【问题描述】:

我有很多标签的应用程序,我想为每个标签添加图像或样式,请问如何?

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

谢谢

【问题讨论】:

  • 为什么他们只是放代码然后离开?请有人帮助我

标签: android android-tabhost android-tabs


【解决方案1】:

试试这个: 从您的 OnCreate() 方法中调用它:

 setTabs();

然后放上这段代码

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

}

然后在drawable文件夹中创建tab_indicator.xml并放入这段代码。在那里你可以设置不同的颜色,当它被按下,聚焦等等......

 <?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 并输入以下代码:

<?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>

【讨论】:

  • 感谢您的回答,我会阅读并应用它,然后告诉您结果如何
  • 在您的代码中 R.layout.tab_indicator 必须是 R.drawable.tab_indicator 对吗?什么是getTabWidget()R.id.titleR.id.icon
  • 感谢您的重播,我会检查
  • 在你的代码中插入 @drawable/tab_unselected@drawable/tab_selected@drawable/tab_focus我必须把我的图像正确吗?
  • 没有方法getTabWidget()
【解决方案2】:

您可以将图像设置为选项卡,如下所示:

tabHost.newTabSpec("All").setIndicator(null,res.getDrawable(R.drawable.icon)).setContent(R.id.tab_template_two_tabs_all);

或者设置样式试试这个:

 tabHost.newTabSpec("All").setIndicator(null,res.getDrawable(R.style.myStyle)).setContent(R.id.tab_template_two_tabs_all);

【讨论】:

  • 如果在片段或活动上,则替换为 getResources。如果使用资源 res = getResources();,它提供对资源的访问
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-01
  • 2014-09-24
  • 1970-01-01
  • 2012-09-27
  • 2021-09-12
相关资源
最近更新 更多