首先,感谢丹尼斯为我安排足够接近正确的解决方案,我可以勉强维持一个工作项目。现在,这应该更接近功能。我将名称更改为通用名称,但希望所有内容都能按预期工作,并且足以帮助您了解正在发生的事情。
TabHostActivity(主活动):
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;
public class TabHostActivity extends FragmentActivity implements TabHost.OnTabChangeListener
{
private static final String TAG = "TabHostActivity";
private TabHost tabHost;
@Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabhost);
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setOnTabChangedListener(this);
tabHost.setCurrentTab(0);
setupTabs();
}
private void setupTabs()
{
tabHost.setup();
setupTab(new TextView(this), "tab1");
setupTab(new TextView(this), "tab2");
setupTab(new TextView(this), "tab3");
setupTab(new TextView(this), "tab4");
}
private void setupTab(final View view, final String tag)
{
View tabview = createTabView(tabHost.getContext(), tag);
TabHost.TabSpec setContent = tabHost.newTabSpec(tag)
.setIndicator(tabview)
.setContent(new TabHost.TabContentFactory()
{
public View createTabContent(String tag)
{
return view;
}
});
tabHost.addTab(setContent);
}
/**
* Return the view for the individual tabs, the tab view being set is identified by tabId.
*
* @param context
* @param tabId
* @return
*/
private static View createTabView(final Context context, final String tabId)
{
int resourceId;
if (tabId.equals("tab1"))
{
resourceId = R.layout.tab1;
}
else if (tabId.equals("tab2"))
{
resourceId = R.layout.tab2;
}
else if (tabId.equals("tab3"))
{
resourceId = R.layout.tab3;
}
else
{
resourceId = R.layout.tab4;
}
return LayoutInflater.from(context).inflate(resourceId, null);
}
@Override
public void onTabChanged(String tabId)
{
Log.d(TAG, "onTabChanged(): tabId=" + tabId);
if (tabId.equalsIgnoreCase("tab1"))
{
updateTab(android.R.id.tabcontent, new tab1(), tabId);
}
else if (tabId.equalsIgnoreCase("tab2"))
{
updateTab(android.R.id.tabcontent, new tab2(), tabId);
}
else if (tabId.equalsIgnoreCase("tab3"))
{
updateTab(android.R.id.tabcontent, new tab3(), tabId);
}
else
{
updateTab(android.R.id.tabcontent, new tab4(), tabId);
}
}
public void updateTab(int placeholder, Fragment fragment, String tabId)
{
getSupportFragmentManager().beginTransaction()
.replace(placeholder, fragment, tabId)
.commit();
}
}
activity_tabhost.xml:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<FrameLayout
android:id="@+id/tab_1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="@+id/tab_2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="@+id/tab_3"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<FrameLayout
android:id="@+id/tab_4"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout >
</TabHost >
tab1.xml,tab2.xml,tab3.xml,tab4.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
tab1.java,tab2.java,tab3.java,tab4.java:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class tab1 extends Fragment
{
private static final String TAG = "tab1";
@Override
public void onActivityCreated(final Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
Log.i(TAG, "onActivityCreated");
}
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState)
{
Log.i(TAG, "onCreateView");
return inflater.inflate(R.layout.tab1, container, false);
}
}
的AndroidManifest.xml:
<activity android:name="<YOUR_PACKAGE_NAME>.TabHostActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
这是使用API作为目标的V19进行,并使用该android.support.v4.app库最低V10的。
所以,再次感谢deniz让我达到这一点,我花了10个小时尝试一些事情,我发现的一切都过时了。希望这可以帮助其他处于我处于不幸状态的人。你现在应该有一个工作状态,并且可以看到你可以设计标签的位置以及你放置碎片内容的位置。
PS - 是的,在我的泛化传递中,它确实导致了xml内容,并且标签被称为“tab1.whatever”,希望这不是太混乱,这可以通过一些上下文来推断。
感谢您的回复。我会试试这个。几个简单的问题。所有这些方法应该在ActivityMain中吗?ActivityMain会延伸什么?信息如何从一个标签传递到下一个? – gotguts
1.all方法可以在maiactivity本身dd,2.extends FragmentActivity 3.你可以保留一个dataprovider类并保存细节 – deniz
即使在创建必要的xml后,我仍然收到一大堆编译器错误,文件 – gotguts