2014-01-31 1 views
0

탭이 포함 된 기본 Android 앱을 만들려고했습니다. 탭을 클릭하면 각 단편이 별도의 XML 파일 인 새로운 단편을로드합니다. Activity Bar 탭이 아닌 TabHost를 사용하고 싶습니다. 나는 많은 튜토리얼을 통해 웹 및 게시물 stackoverflow에 여기를 찾고 있지만 아무 소용이 있습니다. 대부분의 질문 및 자습서에서는 단편 또는 탭 중 하나만을 다루지 만 두 주제는 모두 다루지 않습니다. 미리 제안 해 주셔서 감사합니다.TabHost 및 여러 단편을 사용하여 Android 앱 만들기

I에 포함 된 매우 기본적인 응용 프로그램 시도하고 싶습니다

입력 할 수
  • 한 조각 - XML을 file_1 (1 탭) 텍스트 뷰의 수를 표시하는
  • 한 조각 - XML을 file_2을 (두번째 탭)

답변

0

XML 파일

같은 시도 활동에

mTabHost = (TabHost)findViewById(android.R.id.tabhost); 
     mTabHost.setOnTabChangedListener(this); 
     mTabHost.setCurrentTab(0); 
     setupTabs(); 

setupTabs() 메소드

private void setupTabs() { 
     mTabHost.setup(); 
     setupTab(new TextView(this), "Tab1"); 
     setupTab(new TextView(this), "Tab2"); 
      setupTab(new TextView(this), "Tab3"); 
     mTabHost.getTabWidget().setDividerDrawable(R.drawable.empty);//not necessery 

    } 

setupTab() 메소드 // 설정 하나의 탭

private void setupTab(final View view, final String tag) { 
     View tabview = createTabView(mTabHost.getContext(), tag); 

     TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() { 
      public View createTabContent(String tag) { 
       return view; 
      } 
     }); 
     mTabHost.addTab(setContent); 

    } 

createTabView 방법

private static View createTabView(final Context context, final String text) { 
     int resouceId = R.layout.tabs_bg; 
     if(text.equals("Tab1")) { 
      resouceId = R.layout.tab_1_layout; 
     } else if(text.equals("Tab2")) { 
      resouceId = R.layout.tab_2_layout; 
     } 
     View view = LayoutInflater.from(context).inflate(resouceId, null); 
     return view; 
    } 
을 한 OnCreate3210

onTabChanged 방법

@Override 
    public void onTabChanged(String tabId) { 
     Log.d("TAB_CHANGE","onTabChanged(): tabId=" + tabId); 
     updateTab(tabId, R.id.tab_1,new YourFragment()); 
    } 

public void updateTab(String tabId, int placeholder,Fragment fragment) { 
     FragmentManager fm = getSupportFragmentManager(); 
     fm.beginTransaction() 
     .replace(placeholder,fragment, tabId) 
     .commit(); 
    } 
+0

답장을 보내 주셔서 감사합니다. 나는 이것을 밖으로 시험 할 것이다. 몇 가지 빠른 질문. 이러한 모든 메소드가 ActivityMain에 있어야합니까? ActivityMain은 무엇을 확장할까요? 한 탭에서 다음 탭으로 정보가 어떻게 전달됩니까? – gotguts

+0

1.all 메서드는 maiactivity 자체에서 사용할 수 있습니다. 2.FragmentActivity를 연장합니다. 3. dataprovider 클래스를 유지하고 그 세부 정보를 저장합니다. – deniz

+0

필요한 XML을 만든 후에도 주어진 코드로 컴파일러 오류가 발생했습니다. 파일 – gotguts

0

우선, 내가 작업 프로젝트를 견디다 수있는 적합한 솔루션 충분히 나를 가까워에 대한 니즈에 감사합니다. 이제는 기능에 더 가깝습니다. 이름을 일반 이름으로 변경했지만 모든 것이 의도 한대로 작동하고 어떤 일이 일어나는지 이해하는 데 도움이되기를 바랍니다.

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"라는 결과가 발생했습니다. 조금 혼란 스러울 정도로 혼란스럽지 않은지 잘 모르겠습니다.