2012-09-08 7 views
4

나는 안드로이드 4.1에서 탭이없는 Tabs를 얻을 수있었습니다. 나는 FragmentActivity, fragments 및 actionbar를 사용한다는 것을 의미합니다. 탭이 생겼습니다. 탭을 클릭 할 때마다 각 프래그먼트가로드됩니다. 아니, 내 문제는 구조와 같은 세로 탭에서 같은 필요합니다.안드로이드 수직 동작 바 탭

public class MainActivity extends FragmentActivity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


     ActionBar actionbar = getActionBar(); 
     actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     View view1 = LayoutInflater.from(this).inflate(R.layout.action_bar_layout, null); 
     ((TextView) view1.findViewById(R.id.textView)).setText("Tab1"); 

     ActionBar.Tab tab1 = actionbar.newTab().setCustomView(view1); 

     View view2 = LayoutInflater.from(this).inflate(R.layout.action_bar_layout, null); 
     ((TextView) view2.findViewById(R.id.textView)).setText("Tab2"); 

     ActionBar.Tab tab2 = actionbar.newTab().setCustomView(view2); 

     View view3 = LayoutInflater.from(this).inflate(R.layout.action_bar_layout, null); 
     ((TextView) view3.findViewById(R.id.textView)).setText("Tab3"); 

     ActionBar.Tab tab3 = actionbar.newTab().setCustomView(view3); 

     View view4 = LayoutInflater.from(this).inflate(R.layout.action_bar_layout, null); 
     ((TextView) view4.findViewById(R.id.textView)).setText("Tab4"); 

     ActionBar.Tab tab4 = actionbar.newTab().setCustomView(view4); 

     View view5 = LayoutInflater.from(this).inflate(R.layout.action_bar_layout, null); 
     ((TextView) view5.findViewById(R.id.textView)).setText("Tab5"); 

     ActionBar.Tab tab5 = actionbar.newTab().setCustomView(view5); 

     Fragment fragment1 = new Fragment1(); 
     Fragment fragment2 = new Fragment2(); 
     Fragment fragment3 = new Fragment3(); 
     Fragment fragment4 = new Fragment4(); 
     Fragment fragment5 = new Fragment5(); 

     tab1.setTabListener(new MyTabsListener(fragment1, view1)); 
     tab2.setTabListener(new MyTabsListener(fragment2, view2)); 
     tab3.setTabListener(new MyTabsListener(fragment3, view3)); 
     tab4.setTabListener(new MyTabsListener(fragment4, view4)); 
     tab5.setTabListener(new MyTabsListener(fragment5, view5)); 

     actionbar.addTab(tab1); 
     actionbar.addTab(tab2); 
     actionbar.addTab(tab3); 
     actionbar.addTab(tab4); 
     actionbar.addTab(tab5); 

    } 

class MyTabsListener implements TabListener { 
    public Fragment fragment; 
    public View view; 

    public MyTabsListener(Fragment fragment, View view) { 
     this.fragment = fragment; 
     this.view = view; 
    } 

    @Override 
    public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) { 
     System.out.println("onTabReselected"); 

    } 

    @Override 
    public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) { 

     ((RelativeLayout) view.findViewById(R.id.rootElement)).setBackgroundColor(Color.WHITE); 
     ((TextView) view.findViewById(R.id.textView)).setTextColor(Color.BLACK); 

     FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.mainFragement, fragment); 
     fragmentTransaction.commit(); 

    } 

    @Override 
    public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) { 
     ((RelativeLayout) view.findViewById(R.id.rootElement)).setBackgroundColor(Color.BLACK); 
     ((TextView)view.findViewById(R.id.textView)).setTextColor(Color.WHITE); 
    } 
} 
    } 

그리고 내 activity_main.xml이며,

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
android:layout_gravity="center" 
android:layout_height="fill_parent" 
android:layout_width="fill_parent" 
android:orientation="vertical" 
xmlns:android="http://schemas.android.com/apk/res/android" 
> 
<LinearLayout 
    android:id="@+id/fragment_container" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    > 
    <FrameLayout 
    android:id="@+id/mainFragement" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
</FrameLayout> 
</LinearLayout> 

답변

3

작업 표시 줄이 나는 "구조와 같은 수직 탭을"고려할 것을 지원하지 않습니다. 사실 Android의 경우 '구조와 유사한 세로 형 탭'이 일반적으로 표시되지 않습니다. 당신이 볼 수있는 가장 가까운 것은 왼쪽에 ListView이있는 가로 방향의 태블릿 응용 프로그램에서 오른쪽의 UI 내용을 제어하는 ​​것입니다 (소위 "마스터 세부"패턴).

+0

마스터 - 디테일 패턴을 얻으려는 링크 나 아이디어를 제안 해 주시겠습니까? 마스터 목록에있는 항목을 클릭하면 세부 사항에 조각이로드됩니다. – Neela

+1

@SakthiSai : 당신이 무엇을 찾고 있는지 정확히 모르겠습니다. http://developer.android.com/design/patterns/multi-pane-layouts.html http://developer.android.com/design/patterns/swipe-views.html – CommonsWare