2013-10-24 28 views
-1

我指的是在Android中製作簡單選項卡的this教程。在Android應用程序中使用選項卡

MainActivity.java

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

    @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"); 

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

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

activity_main.xml中

<?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="match_parent" 
    android:layout_height="match_parent" /> 

fragmenttab1.xml

<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="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:text="@string/Fragment1" /> 

</RelativeLayout> 

硅很多其他XML文件。

輸出是:

enter image description here

顯然選項卡不是均勻分佈的,因爲空的空間留在右手邊,一切似乎都推到左上角。 如何使這發生?

+0

參考這個沒有問題 - http://stackoverflow.com/questions/17170942/actionbarsherlock-tabs-customization – VikramV

+1

這取決於你如何附加3個選項卡。但基本上,而不是'android:layout_width =「wrap_content」'你需要使用'android:layout_width =「0dip」android:layout_weight =「1」'。這將動態地告訴選項卡填充寬度,甚至可以調整大小。 (因爲每個標籤的權重是一樣的) – Doomsknight

+0

@ Doomsknight ......我仍然試着讓我有同樣的反應......它是否也依賴於API-13級別&emulator? – smriti3

回答

1

試試這個代碼: -

主要活動

public class MainActivity extends FragmentActivity implements 
    ActionBar.TabListener { 

/** 
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
* one of the primary sections of the app. 
*/ 
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter { 

    public AppSectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public int getCount() { 
     return 4; 
    } 

    @Override 
    public Fragment getItem(int i) { 
     switch (i) { 
     case 0: 
      // The first section of the app is the most interesting -- it 
      // offers 
      // a launchpad into the other demonstrations in this example 
      // application. 
      i++; 
      return new LaunchpadSectionFragment(); 

     case 1: 
      // The first section of the app is the most interesting -- it 
      // offers 
      // a launchpad into the other demonstrations in this example 
      // application. 
      i++; 
      return new LaunchpadSectionFragment(); 
     case 2: 
      // The first section of the app is the most interesting -- it 
      // offers 
      // a launchpad into the other demonstrations in this example 
      // application. 
      i++; 
      return new LaunchpadSectionFragment(); 
     case 3: 
      // The first section of the app is the most interesting -- it 
      // offers 
      // a launchpad into the other demonstrations in this example 
      // application. 
      i++; 
      return new LaunchpadSectionFragment(); 
     case 4: 
      // The first section of the app is the most interesting -- it 
      // offers 
      // a launchpad into the other demonstrations in this example 
      // application. 
      i++; 
      return new LaunchpadSectionFragment(); 

     default: 
      // The other sections of the app are dummy placeholders. 
      Fragment fragment = new DummySectionFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1); 
      fragment.setArguments(args); 
      return fragment; 
     } 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     String name = null; 
     switch (position) { 
     case 0: 
      name = "School Info"; 
      break; 
     case 1: 
      name = "Picture (s)"; 
      break; 

     default: 
      name = "Group"; 
      break; 
     } 
     return name; 
    } 
} 

/** 
* A dummy fragment representing a section of the app, but that simply 
* displays dummy text. 
*/ 
public static class DummySectionFragment extends Fragment { 

    public static final String ARG_SECTION_NUMBER = "section_number"; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_section_dummy, 
       container, false); 
     Bundle args = getArguments(); 
     ((TextView) rootView.findViewById(android.R.id.text1)) 
       .setText(getString(R.string.dummy_section_text, 
         args.getInt(ARG_SECTION_NUMBER))); 
     return rootView; 
    } 
} 

/** 
* A fragment that launches other parts of the demo application. 
*/ 
public static class LaunchpadSectionFragment extends Fragment { 

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

     // Demonstration of a collection-browsing activity. 
     rootView.findViewById(R.id.demo_collection_button) 
       .setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         Intent intent = new Intent(getActivity(), 
           CollectionDemoActivity.class); 
         startActivity(intent); 
        } 
       }); 

     // Demonstration of navigating to external activities. 
     rootView.findViewById(R.id.demo_external_activity) 
       .setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         // Create an intent that asks the user to pick a 
         // photo, but using 
         // FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET, ensures that 
         // relaunching 
         // the application from the device home screen does 
         // not return 
         // to the external activity. 
         // Intent externalActivityIntent = new Intent(
         // Intent.ACTION_PICK); 
         // externalActivityIntent.setType("image/*"); 
         // externalActivityIntent 
         // .addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 
         // startActivity(externalActivityIntent); 

         System.exit(0); 
        } 
       }); 

     return rootView; 
    } 
} 

/** 
* The {@link android.support.v4.view.PagerAdapter} that will provide 
* fragments for each of the three primary sections of the app. We use a 
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which 
* will keep every loaded fragment in memory. If this becomes too memory 
* intensive, it may be best to switch to a 
* {@link android.support.v4.app.FragmentStatePagerAdapter}. 
*/ 
AppSectionsPagerAdapter mAppSectionsPagerAdapter; 

/** 
* The {@link ViewPager} that will display the three primary sections of the 
* app, one at a time. 
*/ 
ViewPager mViewPager; 

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

    // Create the adapter that will return a fragment for each of the three 
    // primary sections 
    // of the app. 
    mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(
      getSupportFragmentManager()); 

    // Set up the action bar. 
    final ActionBar actionBar = getActionBar(); 

    // Specify that the Home/Up button should not be enabled, since there is 
    // no hierarchical 
    // parent. 
    actionBar.setHomeButtonEnabled(false); 

    // Specify that we will be displaying tabs in the action bar. 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

    // Set up the ViewPager, attaching the adapter and setting up a listener 
    // for when the 
    // user swipes between sections. 
    mViewPager = (ViewPager) findViewById(R.id.pager); 
    mViewPager.setAdapter(mAppSectionsPagerAdapter); 
    mViewPager 
      .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
       @Override 
       public void onPageSelected(int position) { 
        // When swiping between different app sections, select 
        // the corresponding tab. 
        // We can also use ActionBar.Tab#select() to do this if 
        // we have a reference to the 
        // Tab. 
        actionBar.setSelectedNavigationItem(position); 
       } 
      }); 

    // For each of the sections in the app, add a tab to the action bar. 
    for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) { 
     // Create a tab with text corresponding to the page title defined by 
     // the adapter. 
     // Also specify this Activity object, which implements the 
     // TabListener interface, as the 
     // listener for when this tab is selected. 
     actionBar.addTab(actionBar.newTab() 
       .setText(mAppSectionsPagerAdapter.getPageTitle(i)) 
       .setTabListener(this)); 
    } 
} 

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

} 

@Override 
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) { 
    // TODO Auto-generated method stub 
    // When the given tab is selected, switch to the corresponding page in 
    // the ViewPager. 
    mViewPager.setCurrentItem(tab.getPosition()); 
} 

@Override 
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) { 
    // TODO Auto-generated method stub 

} 

} 

CollectionDemoActivity

public class CollectionDemoActivity extends FragmentActivity { 

/** 
* A {@link android.support.v4.app.FragmentStatePagerAdapter} that returns a 
* fragment representing an object in the collection. 
*/ 
public static class DemoCollectionPagerAdapter extends 
     FragmentStatePagerAdapter { 

    public DemoCollectionPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public int getCount() { 
     // For this contrived example, we have a 10-object collection. 
     return 10; 
    } 

    @Override 
    public Fragment getItem(int i) { 
     Fragment fragment = new DemoObjectFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1); // Our object is 
                  // just an 
                  // integer :-P 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return "Q. Set " + (position + 1); 
    } 
} 

/** 
* A dummy fragment representing a section of the app, but that simply 
* displays dummy text. 
*/ 
public static class DemoObjectFragment extends Fragment { 

    public static final String ARG_OBJECT = "Question Set"; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(
       R.layout.fragment_collection_object, container, false); 
     Bundle args = getArguments(); 
     ((TextView) rootView.findViewById(android.R.id.text1)) 
       .setText("Q.Set " 
         + Integer.toString(args.getInt(ARG_OBJECT))); 
     return rootView; 
    } 
} 

/** 
* The {@link android.support.v4.view.PagerAdapter} that will provide 
* fragments representing each object in a collection. We use a 
* {@link android.support.v4.app.FragmentStatePagerAdapter} derivative, 
* which will destroy and re-create fragments as needed, saving and 
* restoring their state in the process. This is important to conserve 
* memory and is a best practice when allowing navigation between objects in 
* a potentially large collection. 
*/ 
DemoCollectionPagerAdapter mDemoCollectionPagerAdapter; 

/** 
* The {@link android.support.v4.view.ViewPager} that will display the 
* object collection. 
*/ 
ViewPager mViewPager; 

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

    // Create an adapter that when requested, will return a fragment 
    // representing an object in 
    // the collection. 
    // 
    // ViewPager and its adapters use support library fragments, so we must 
    // use 
    // getSupportFragmentManager. 
    mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter(
      getSupportFragmentManager()); 

    // Set up action bar. 
    final ActionBar actionBar = getActionBar(); 

    // Specify that the Home button should show an "Up" caret, indicating 
    // that touching the 
    // button will take the user one step up in the application's hierarchy. 
    actionBar.setDisplayHomeAsUpEnabled(true); 

    // Set up the ViewPager, attaching the adapter. 
    mViewPager = (ViewPager) findViewById(R.id.pager); 
    mViewPager.setAdapter(mDemoCollectionPagerAdapter); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case android.R.id.home: 
     // This is called when the Home (Up) button is pressed in the action 
     // bar. 
     // Create a simple intent that starts the hierarchical parent 
     // activity and 
     // use NavUtils in the Support Package to ensure proper handling of 
     // Up. 
     Intent upIntent = new Intent(this, MainActivity.class); 
     if (NavUtils.shouldUpRecreateTask(this, upIntent)) { 
      // This activity is not part of the application's task, so 
      // create a new task 
      // with a synthesized back stack. 
      TaskStackBuilder.from(this) 
      // If there are ancestor activities, they should be added here. 
        .addNextIntent(upIntent).startActivities(); 
      finish(); 
     } else { 
      // This activity is part of the application's task, so simply 
      // navigate up to the hierarchical parent activity. 
      NavUtils.navigateUpTo(this, upIntent); 
     } 
     return true; 

    case R.id.menuCancel: 
     finish(); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
} 
0

這取決於Ø n個仿真器 - 我的意思是在模擬器中設備的名稱

嘗試另一種不同的API級別

有一個與代碼

相關問題