2014-03-02 2 views
2

내 Android 용 Fragment에 탭을 추가하려면 Android 용 TabHost를 사용하고 있습니다. 내 문제는 탭이 내 XML 위에 있다는 것입니다. 내 조각 XML을 자동으로 탭에서 시작할 것이라고 생각합니까?의 내용은 내 xml의 텍스트 상단에 있습니다.

public class StatisticsTab extends Fragment { 


    private FragmentTabHost mTabHost; 

    //Mandatory Constructor 
    public StatisticsTab() { 
    } 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

    } 

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

     View rootView = inflater.inflate(R.layout.fragment_tabs,container, false); 


     mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost); 
     mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent); 

     mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Fragment B"), 
       StatisticsPage.class, null); 
     mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Fragment C"), 
       BreweryStatistics.class, null); 
     mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("Fragment D"), 
       StyleStatistics.class, null); 


     return rootView; 
    } 




} 

여기 탭에서 내 조각 중 하나에 대한 코드입니다 :이 탭에 대한

enter image description here

내 코드는

public class StyleStatistics extends Fragment implements GetStyleStatisticsJSON.OnArticleSelectedListener { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.style_statistics_layout, container, false); 

     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
     String userName = prefs.getString("userName", null); 
     String userID = prefs.getString("userID", null); 


     String url = "myURL"; 

     //async task to get beer taste tag percents 
     GetStyleStatisticsJSON task = new GetStyleStatisticsJSON(getActivity()); 
     task.setOnArticleSelectedListener(this); 
     task.execute(url); 

     // Inflate the layout for this fragment 
     return v; 

    } 


    @Override 
    public void onArticleSelected(String bID){ 

     //code to execute on click 
     Fragment Fragment_one; 
     FragmentManager man= getFragmentManager(); 
     FragmentTransaction tran = man.beginTransaction(); 
     Fragment_one = new StylePage2(); 
     final Bundle bundle = new Bundle(); 
     bundle.putString("beerIDSent", bID); 
     Fragment_one.setArguments(bundle); 
     tran.replace(R.id.main, Fragment_one);//tran. 
     tran.addToBackStack(null); 
     tran.commit(); 

    } 


} 

는 여기에 코드입니다 단편 xml :

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

    <TextView 
     android:id="@+id/styleStatsTitle" 
     android:gravity="center" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:text="Your Top Styles" 
     android:textSize="20sp" 
     android:textStyle = "bold" 
     android:padding="5dip" 
     > 
    </TextView> 

    <ListView 
     android:id="@+id/yourStyleStatistics" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:dividerHeight="0px" 
     android:divider="@null" 

     > 
    </ListView> 

</LinearLayout> 

fragment_tab.xml :

<android.support.v4.app.FragmentTabHost 
    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:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <FrameLayout 
      android:id="@+id/realtabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"/> 





    </LinearLayout> 
</android.support.v4.app.FragmentTabHost> 
+1

, fragment_tabs.xml – Aashir

+0

가 @Aashir 내 질문을 업데이트하고 XML 최고 – Mike

답변

2

당신은 아마 당신의 fragment_tabs.xmlFrameLayout 위의 TabWidget 필요, 예 :

<android.support.v4.app.FragmentTabHost 
    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:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0"/> 

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

     <FrameLayout 
      android:id="@+id/realtabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"/> 

    </LinearLayout> 
</android.support.v4.app.FragmentTabHost> 
+0

을 게시 게시하시기 바랍니다 지금 이것을 시도하고 무슨 일이 일어나는 지 봅니다. – Mike

+0

시도했지만이 오류가 발생했습니다 : https://gist.github.com/anonymous/4775120371eb041522c8 – Mike

+1

내 대답을 편집하고 ID가'@android : id/tabcontent' 인'FrameLayout'을 추가했습니다. 파편에 realtabcontent를 사용하면 비어 있습니다. 도움이되는지 확인하십시오. – Leszek