我想在选项卡中添加选择器。喜欢每个标签的select_image和unselect_image。我是这样做的。
此特定代码正在为TabActivity
运行public class MainActivity extends FragmentActivity
{
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
TabSpec spec;
Intent intent;
View saveView = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab_save_btn,null);
intent = new Intent(MainActivity.this, Save.class);
spec = mTabHost.newTabSpec("SAVE").setIndicator(saveView)
.setContent(intent);
mTabHost.addTab(spec);
View chargeView = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab_charge_btn, null);
intent = new Intent(MainActivity.this, Charge.class);
spec = mTabHost.newTabSpec("CHARGE").setIndicator(chargeView)
.setContent(intent);
mTabHost.addTab(spec);
View rankView = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab_rank_btn, null);
intent = new Intent(MainActivity.this, RankFragment.class);
spec = mTabHost.newTabSpec("RANK").setIndicator(rankView)
.setContent(intent);
mTabHost.addTab(spec);
mTabHost.setCurrentTab(1);
}
}
tab_save_btn.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/save_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/save_selector" />
</RelativeLayout>
save_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/home_selected" android:state_selected="true"></item>
<item android:drawable="@drawable/home_unselect" android:state_selected="false"></item>
</selector>
它给了我多个错误。 TabActivity运行相同的代码。
您是否忘记调用tabhost.setup(LocalActivityManager activityGroup)
我在哪里做错了?
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
答案 0 :(得分:4)
试试这个
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
View saveView = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab_save_btn,null);
mTabHost.addTab(mTabHost.newTabSpec("SAVE").setIndicator(saveView ),SaveFragment.class, null);
View chargeView = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab_charge_btn, null);
mTabHost.addTab(mTabHost.newTabSpec("CHARGE").setIndicator(chargeView ),ChargeFragment.class, null);
.....
...........
mTabHost.setCurrentTab(1);