我正在尝试在android中制作一个带有图标和背景的tabview。但无法设置选项卡的背景。
的java
intent = new Intent().setClass(this, mainActivity.class);
spec = tabHost.newTabSpec("main").setIndicator("Main", res.getDrawable(R.drawable.ic_tab_main)).setContent(intent);
tabHost.addTab(spec);
XML
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/cameraiconblack"
android:state_selected="true" />
<item android:drawable="@drawable/cameraiconwhite" />
</selector>
答案 0 :(得分:2)
提供此
<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" />
然后在tab_unselected.xml中写
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#ccc"/>
</shape>
&安培; for tab_selected.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#ccc"/>
</shape>
现在为背景制作图像和textview的布局文件或任何你想要的
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="64dip"
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"
/>
<TextView android:id="@+id/title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
style="?android:attr/tabWidgetStyle"
/>
现在像这样更改主标签文件
将setTabs()方法放在你的oncreate中。
private void setTabs() {
// TODO Auto-generated method stub
tabHost = getTabHost();
tabHost.getTabWidget().setDividerDrawable(R.drawable.line);
intent = new Intent(this, class1.class);
addTab(1,intent,"tab1");
intent=new Intent(this,class2.class);
addTab(2,intent,"tab2");
intent=new Intent(this,class3.class);
addTab(3,intent,"tab3");
}
现在addTab方法
private void addTab(int labelId, Intent intent, String tabName) {
// TODO Auto-generated method stub
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.title1);
title.setTextSize(40);
title.setText(tabName);
ImageView imageView=(ImageView)tabIndicator.findViewById(R.id.icon);
imageView.setImageResource(R.drawable.aries);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
我想我已经把所有代码都需要了。现在享受
答案 1 :(得分:0)
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent;
查看view_temples = LayoutInflater.from(getBaseContext())。inflate(R.layout.tab_view,null);
spec = tabHost.newTabSpec(“main”)。setIndicator(view_temples.findViewById(R.id.your_image_layout_temple)) .setContent(意向); tabHost.addTab(SPEC);