我正在尝试创建一个在底部有标签的页面,我们可以通过单击选项卡导航到不同的页面。 我可以使用android中的tabhosts小部件正确实现此选项卡。 (我知道android不希望我们实现基于底部的选项卡。)
当其中一个正在打开的活动中有另一个tabhost时,就会出现问题,也就是说嵌套的tab实现。
链接到外部tabhost上的内部tabhost覆盖的活动,当我在内部tabhost的其中一个选项卡上时,我无法单击外部tabhost。
外部tabhost确实与内部tabhost不同。
我已经没有想法了。我需要帮助。
我也试过实现FragmentTabHost但结果相同。我想要一种从内部tabhost访问外部tabhost的方法。
我的主要活动文件包含外部tabhost:Welcome_page.java
package com.ciaoappskeleton.login;
import android.app.Activity;
import android.app.LocalActivityManager;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import com.ciaoappskeleton.CallTab;
import com.ciaoappskeleton.R;
public class Welcome_page extends TabActivity {
private Context ctx=this;
TabHost tabHost;
private Resources res;
private LocalActivityManager lam;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_page);
lam = new LocalActivityManager(this, false);
lam.dispatchCreate(savedInstanceState);
res=getResources();
initialize_tabs(); //initialize the tabs
}
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
return true;
}
public void initialize_tabs()
{
tabHost = (TabHost) findViewById(android.R.id.tabhost);
if(tabHost==null)
{
Log.i("TABHOST status 1","is null");
return;
}
tabHost.setup(lam);
addTab(R.drawable.nav_phone_big,com.ciaoappskeleton.CallTab.class,"Calls","CALLS");
/*
this CallTab.java activity contains another tabhost
*/
addTab(R.drawable.nav_message,com.ciaoappskeleton.MessagesTab.class,"SMS","MESSAGES");
addTab(R.drawable.nav_coin,CoinDisplay.class,"Credits","CREDITS");
addTab(R.drawable.nav_gear,GearDisplay.class,"Account","ACCOUNTS");
tabHost.setCurrentTab(0);
tabHost.getTabWidget().getChildTabViewAt(0).setVisibility(View.GONE);
/* just setting the background for the tabs */
//set the current tab background to ciao background
int curr_tab=tabHost.getCurrentTab();
tabHost.getTabWidget().getChildAt(curr_tab)
.setBackgroundColor(res.getColor(R.color.ciao_color)); // selected
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(res.getColor(R.color.nav_bar_bg));
// unselected
}
tabHost.getTabWidget().getChildAt(curr_tab)
.setBackgroundColor(res.getColor(R.color.ciao_color)); // selected
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(res.getColor(R.color.nav_bar_bg));
// unselected
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
.setBackgroundColor(res.getColor(R.color.ciao_color)); // selected
}
});
}
private void addTab(int drawableId, Class<?> c, String labelId, String imageText)
{
TabSpec tab1 = tabHost.newTabSpec(labelId);
Intent i1=new Intent().setClass(ctx,c);
if(drawableId==-1)
{
String tabIndicator="NA";
tab1.setIndicator(tabIndicator);
tab1.setContent(i1);
tabHost.addTab(tab1);
}
else
{
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_image_setter, getTabWidget(), false);
ImageView iv=(ImageView) tabIndicator.findViewById(R.id.icon);
iv.setImageResource(drawableId);
TextView tv=(TextView) tabIndicator.findViewById(R.id.title);
tv.setText(imageText);
tab1.setIndicator(tabIndicator);
tab1.setContent(i1);
tabHost.addTab(tab1);
}
}
private void set_current_tab(int tabnumber)
{
tabHost.setCurrentTab(tabnumber);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
lam.dispatchResume();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
lam.dispatchPause(isFinishing());
}
}
我的第二项活动:CallTab.java 我只发布重要的方法,初始化和声明已经做得很好
public void initialize_tabs()
{
//tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost=(TabHost) findViewById(android.R.id.tabhost);
if(tabHost==null)
{
Log.i("TABHOST status","is null");
return;
}
tabHost.setup(lam);
addTab(R.drawable.call_tab_keypad,DiallerActivity.class,"Calls","KEYPAD");
addTab(R.drawable.call_tab_contacts,ContactList.class,"Contacts","CONTACTS");
tabHost.setCurrentTab(0);
//set the current tab background to red background
int curr_tab=tabHost.getCurrentTab();
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(res.getColor(R.color.nav_bar_bg));
// unselected
}
tabHost.getTabWidget().getChildAt(curr_tab)
.setBackgroundColor(res.getColor(R.color.ciao_color)); // selected
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(res.getColor(R.color.nav_bar_bg));
// unselected
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab())
.setBackgroundColor(res.getColor(R.color.ciao_color)); // selected
}
});
}
private void addTab(int drawableId, Class<?> c, String labelId, String imageText)
{
TabSpec tab1 = tabHost.newTabSpec(labelId);
Intent i1=new Intent(getApplicationContext(),c);
if(drawableId==-1)
{
String tabIndicator="NA";
tab1.setIndicator(tabIndicator);
tab1.setContent(i1);
tabHost.addTab(tab1);
}
else
{
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_image_setter, tabHost.getTabWidget(), false);
ImageView iv=(ImageView) tabIndicator.findViewById(R.id.icon);
iv.setImageResource(drawableId);
TextView tv=(TextView) tabIndicator.findViewById(R.id.title);
tv.setText(imageText);
tab1.setIndicator(tabIndicator);
tab1.setContent(i1);
tabHost.addTab(tab1);
}
}
布局的XML文件:activity_welcome_page.xml
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="${packageName}.${activityClass}"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</RelativeLayout>
</TabHost>
代码工作正常,但是当我单击内部tabhost中的CONTACTS选项卡时,显示本机联系人的活动已加载,但它被加载到外部tabhost之上,所以我可以看到外部tabhost但我无法点击它。我希望能够点击它。