将选择器图像添加到 fragmentTabHost
Add selector image to fragmentTabHost
我想给选项卡添加选择器。就像每个选项卡的 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 必须调用带有上下文和
FragmentManager 作为参数。
你是不是忘了调用 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>
试试这个
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);
我想给选项卡添加选择器。就像每个选项卡的 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 必须调用带有上下文和 FragmentManager 作为参数。
你是不是忘了调用 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>
试试这个
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);