2013-04-30 68 views
0

你好嗎? 我剛剛發現了一個代碼,讓我創建選項卡,並瀏覽我的Android應用程序中的選項卡......但實際上我遇到了一個小問題 在此代碼中選項卡的名稱是Section +其編號位置 我想知道我可以改變每個標籤的名稱,每個名稱可以是不同的android查看呼叫器編輯代碼

這裏的代碼編碼:

package com.example.android.effectivenavigation; 

import android.app.ActionBar; 
import android.app.FragmentTransaction; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

public class MainActivity extends FragmentActivity implements ActionBar.TabListener { 


AppSectionsPagerAdapter mAppSectionsPagerAdapter; 
ViewPager mViewPager; 

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

    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 onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
} 

@Override 
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
    // When the given tab is selected, switch to the corresponding page in the ViewPager. 
    mViewPager.setCurrentItem(tab.getPosition()); 
} 

@Override 
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
} 

/** 
* 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 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. 
       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 int getCount() { 
     return 3; 
    } 

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

/** 
* 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); 
        } 
       }); 

     return rootView; 
    } 
} 

/** 
* 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; 
    } 
} } 

回答

0

標籤標題顯示爲Section + its number,因爲你是在創建標籤時設置這種方式您代碼 - .setText(mAppSectionsPagerAdapter.getPageTitle(i))。看下面...

// 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)) //this is where the title is set 
         .setTabListener(this)); 
    } 

設置文本.setText()作爲您的願望。

例如,一個簡單的方法是檢查計數器並設置適當的標題...

for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) { 
    // one simple way 
    if(i == 0) { 
     actionBar.addTab(actionBar.newTab() 
      .setText("First Tab") 
      .setTabListener(this)); 
    } 
} 

ActionBar DOCS