2013-10-17 26 views
0

我是Android新手。我想創建一個應用程序,顯示帶有固定選項卡的操作欄。我已經做到了。 問題在於選項卡不適合相同的視圖。固定標籤在同一視圖

這是我的主要活動:

package com.pestana.pestana; 


import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.app.ActionBar; 
import android.app.Fragment; 


public class MainActivity extends Activity { 
    // Declare Tab Variable 
    ActionBar.Tab Tab1, Tab2, Tab3,Tab4; 
    Fragment fragmentTab1 = new FragmentTab1(); 
    Fragment fragmentTab2 = new FragmentTab2(); 
    Fragment fragmentTab3 = new FragmentTab3(); 
    Fragment fragmentTab4 = new FragmentTab4(); 

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

     ActionBar actionBar = getActionBar(); 

     // Hide Actionbar Icon 
     //actionBar.setDisplayShowHomeEnabled(false); 

     // Hide Actionbar Title 
     //actionBar.setDisplayShowTitleEnabled(false); 

     // Create Actionbar Tabs 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     // Set Tab Icon and Titles 
     Tab1 = actionBar.newTab().setIcon(R.drawable.tab1); 
     Tab2 = actionBar.newTab().setText("Tab2"); 
     Tab3 = actionBar.newTab().setText("Tab3"); 
     Tab4 = actionBar.newTab().setText("Tab4"); 

     // Set Tab Listeners 
     Tab1.setTabListener(new TabListener(fragmentTab1)); 
     Tab2.setTabListener(new TabListener(fragmentTab2)); 
     Tab3.setTabListener(new TabListener(fragmentTab3)); 
     Tab4.setTabListener(new TabListener(fragmentTab4)); 

     // Add tabs to actionbar 
     actionBar.addTab(Tab1); 
     actionBar.addTab(Tab2); 
     actionBar.addTab(Tab3); 
     actionBar.addTab(Tab4); 
    } 

    @Override public boolean onCreateOptionsMenu(Menu menu) { 
     super.onCreateOptionsMenu(menu); 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.main, menu); 
     return true; /** true -> el menú ya está visible */ 
    } 
} 

這是佈局

`

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

`

我還創建了4類FragmentTab1 .. 。FragmentTab4,

FragmentTab1

package com.pestana.pestana; 

import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.app.Fragment; 

public class FragmentTab1 extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragmenttab1, container, false); 
     return rootView; 
    } 

} 

FragmentTab2

package com.pestana.pestana; 

import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.app.Fragment; 

public class FragmentTab2 extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragmenttab2, container, false); 
     return rootView; 
    } 

} 

FragmentTab3

package com.pestana.pestana; 

import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.app.Fragment; 

public class FragmentTab3 extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragmenttab3, container, false); 
     return rootView; 
    } 

} 

FragmentTab4

package com.pestana.pestana; 

import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.app.Fragment; 

public class FragmentTab4 extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragmenttab4, container, false); 
     return rootView; 
    } 

} 

TabListener.java

0123每個片段
package com.pestana.pestana; 


import android.app.ActionBar; 
import android.app.ActionBar.Tab; 
import android.app.Fragment; 
import android.app.FragmentTransaction; 

public class TabListener implements ActionBar.TabListener { 

    Fragment fragment; 

    public TabListener(Fragment fragment) { 
     // TODO Auto-generated constructor stub 
     this.fragment = fragment; 
    } 

    @Override 
    public void onTabSelected(Tab tab, FragmentTransaction ft) { 
     // TODO Auto-generated method stub 
     ft.replace(R.id.fragment_container, fragment); 
    } 

    @Override 
    public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
     // TODO Auto-generated method stub 
     ft.remove(fragment); 
    } 

    @Override 
    public void onTabReselected(Tab tab, FragmentTransaction ft) { 
     // TODO Auto-generated method stub 

    } 
} 

4 XML佈局,這是一個例子,只需要改變片段1每個XML

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

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:text="@string/Fragment1" /> 

</RelativeLayout> 

我被困了3天,任何幫助,請?使用相同的代碼,我可以使用不同的選項卡實施滑動視圖?在此先感謝

+0

這是什麼意思'問題是標籤不適合相同的視圖'? – GrIsHu

+1

如果你想用不同的選項卡實現Swipe視圖,那麼你可以去'ViewPager'。 – GrIsHu

回答