우선, 내가 작업 프로젝트를 견디다 수있는 적합한 솔루션 충분히 나를 가까워에 대한 니즈에 감사합니다. 이제는 기능에 더 가깝습니다. 이름을 일반 이름으로 변경했지만 모든 것이 의도 한대로 작동하고 어떤 일이 일어나는지 이해하는 데 도움이되기를 바랍니다.
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의 최소했다.
다시 말해서 나를 데니즈에게 데려다 주신 데 대해 감사 드리며, 10 시간 동안 일을 시도하고 발견 한 모든 것을 오래되어 보았습니다. 바라기를 이것은 나의 이전 상태 인 림보 (limbo)에있는 다른 사람들을 도울 수 있기를 바랍니다.이제 작업 상태가 유지되어야하며 탭을 디자인 할 수있는 위치와 조각의 내용을 넣을 위치를 볼 수 있습니다.
PS - 예, 내 일반화 패스에서 XML 내용과 탭이 "tab1.whatever"라는 결과가 발생했습니다. 조금 혼란 스러울 정도로 혼란스럽지 않은지 잘 모르겠습니다.
답장을 보내 주셔서 감사합니다. 나는 이것을 밖으로 시험 할 것이다. 몇 가지 빠른 질문. 이러한 모든 메소드가 ActivityMain에 있어야합니까? ActivityMain은 무엇을 확장할까요? 한 탭에서 다음 탭으로 정보가 어떻게 전달됩니까? – gotguts
1.all 메서드는 maiactivity 자체에서 사용할 수 있습니다. 2.FragmentActivity를 연장합니다. 3. dataprovider 클래스를 유지하고 그 세부 정보를 저장합니다. – deniz
필요한 XML을 만든 후에도 주어진 코드로 컴파일러 오류가 발생했습니다. 파일 – gotguts