简体   繁体   中英

Custom Action bar in Android?

I have developed an Android application which has 4 TabHost's which are in fragments.

I Know how to customize the ActionBar in MainActivity.

But problem is how can I customize my ActionBar according to the 4 different TabHost's in different Fragment s?

Here is my tabHost code -

public class MainActivity extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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


    /** Creating ANDROID Tab */
    Tab tab = actionBar.newTab()
            //.setText("Android")

            .setTabListener(new CustomTabListener<PlayFragment01>(this, "play", 
    PlayFragment01.class))
            .setIcon(R.drawable.playtabhosticon);

    actionBar.addTab(tab);


    /** Creating APPLE Tab */
    Tab tab1 = actionBar.newTab()
            //.setText("Apple")
            .setTabListener(new CustomTabListener<VenueFragment01>(this, "venu", 
    VenueFragment01.class))
            .setIcon(R.drawable.venutabhosticon);

    actionBar.addTab(tab1);  

    /** Creating APPLE Tab */
    Tab tab3 = actionBar.newTab()
            //.setText("Apple")
            .setTabListener(new CustomTabListener<SocialFragment01>(this, "social", SocialFragment01.class))
            .setIcon(R.drawable.socialtabhosticon);

    actionBar.addTab(tab3);  

    /** Creating APPLE Tab */
    Tab tab4 = actionBar.newTab()
            //.setText("Apple")
            .setTabListener(new CustomTabListener<ActivityFragment01>(this, "activity",         
   ActivityFragment01.class))
            .setIcon(R.drawable.actionbartabhosticon);

    actionBar.addTab(tab4);  



}   
}

Here is my first fragment code -

package in.wptrafficanalyzer.actionbarnavtab;
 /** This is a listfragment class */
 public class PlayFragment01 extends Fragment {

/** An array of items to display in ArrayList */



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)    
{
    /** Creating array adapter to set data in listview */
    View rootView = inflater.inflate(R.layout.fragment_play, container, false);         
    //new DownloadJSON().execute();

    ActionBar actionBar = getActivity().getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    getActivity().getActionBar().setBackgroundDrawable(
            new ColorDrawable(Color.parseColor("#0077d1")));
    ActionBar mActionBar = getActivity().getActionBar();
    getActivity().getActionBar().setIcon(
       new ColorDrawable(getResources().getColor(android.R.color.transparent)));    
    mActionBar.setDisplayShowHomeEnabled(true);
    mActionBar.setDisplayShowTitleEnabled(false);
    LayoutInflater mInflater = LayoutInflater.from(this);
    View mCustomView = mInflater.inflate(R.layout.custom_actionbar2, null);
    mActionBar.setCustomView(mCustomView);
    mActionBar.setDisplayShowCustomEnabled(true);

    return rootView;
}
}

您提出的问题不是很清楚,但是您可以使用getActivity().getActionBar()从Fragment中获取ActionBar实例。

ActionBar.setNavigationMode() and TabHost is now deprecated.

The new ToolBar introduced in API-21 is more powerfull.

But since you asked for a solution, here is a simple one from Google SlidingTabLayout : This is like a PagerTabStrip that you can easily customize and works like great.

It works with ViewPager and Fragments of course.

In my point of view ,you should read this ,so that you can get the basic idea for creating a fragment.

http://developer.android.com/guide/components/fragments.html

Try this sample code..this sample code includes 3 tab ,first try to understand this code ,then you can make your own app with 4 tabs

http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

Also try to modify your question ,It is unclear and also it will be good that you post your errors

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM