2013-08-14 3 views
3

나는 안드로이드에 탭을 생성하고 싶지만 정말 내가 만드는 방법을 모르고
단지 일부 비디오 다운로드하지만 정말이없는 내가 불행하게도 TabActivity를 사용하여 도움이 입니다 가되지 않는 것 :( 그래서 내가 당신을 필요로 어떤 제안 당신은 설치 조각을 실행하는 FragmentActivity 메인 클래스를해야TabActivity 또는를 사용하여 안드로이드에 탭을 만드는 방법 FragmentActivity

답변

0

에 대한 감사를 나를 도와.

을 당신은 지원 라이브러리에서 사용할 수있는 FragmentTabHost 클래스를 사용할 필요가 android.support.v4.app

FragmentTabHost를 사용하는 클래스 :

public class SampleTabFragment extends Fragment { 

private FragmentTabHost mTabHost; 
public static View view; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    // fragment not when container null 
    if (container == null) { 
     return null; 
    } 

    // inflate view from layout 
    view = (LinearLayout) inflater.inflate(R.layout.offer_tabs, container, false); 

    // Setting Up the TabHost 
    mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost); 
    mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.offer_realtabcontent); 

    // For adding a tab divider image 
    mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider); 

    // For Setting up the Tabs with their respective labels 
    mTabHost.addTab(mTabHost.newTabSpec("info").setIndicator(createTabView 
      (mTabHost.getContext(), "INFO")), DetailedOfferFragment.class, null); 
    mTabHost.addTab(mTabHost.newTabSpec("location").setIndicator(createTabView 
      (mTabHost.getContext(), "LOCATION")), LocationFragment.class, null); 

    setRetainInstance(true); 
    return view; 
} 

/***** Method for Custom TextView for Tabs *****/ 
private static View createTabView(final Context context, final String text) { 
    View view = LayoutInflater.from(context).inflate(R.layout.tabs_text_bg, null); 
    TextView tv = (TextView) view.findViewById(R.id.tabsText); 
    tv.setText(text); 
    return view; 
} 
} 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="#6D6C6C" 
android:orientation="vertical" > 

<android.support.v4.app.FragmentTabHost 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     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_weight="0" 
      android:orientation="horizontal" /> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="2dp" 
      android:background="#696969" /> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_weight="0" /> 

     <FrameLayout 
      android:id="@+id/offer_realtabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 
    </LinearLayout> 
</android.support.v4.app.FragmentTabHost> 
</LinearLayout> 
offer_tabs.xml 탭을 사용하는 레이아웃