2012-09-08 36 views
4

我已经设法在android 4.1中得到Tabs Without Tabactivity。我的意思是使用FragmentActivity,分段和操作栏。我得到标签每当我点击任何标签,它加载每个片段。 NoW我的问题是我需要像结构一样的垂直标签。Android的垂直行动栏标签

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