Custom TabWidget Android Tab Indicator

大兔子大兔子 提交于 2019-12-06 07:58:25

okay i found a solution. here is the code:

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class Cook extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cook_layout);

  TabHost tabHost = getTabHost();

    // Tab for Snacks
    TabSpec snackspec = tabHost.newTabSpec("Snacks");
    // setting Title and Icon for the Tab
    snackspec.setIndicator(makeTabIndicator(getResources().getDrawable(R.drawable.cook_icon_tab_snacks)));
    Intent snacksIntent = new Intent(this, Cook_tab_snacks.class);
    snackspec.setContent(snacksIntent);



    // Tab for Mains
    TabSpec mainspec = tabHost.newTabSpec("Mains");
    mainspec.setIndicator(makeTabIndicator( getResources().getDrawable(R.drawable.cook_icon_tab_snacks)));
    Intent mainsIntent = new Intent(this, Cook_tab_mains.class);
    mainspec.setContent(mainsIntent);

    // Tab for Desserts
    TabSpec dessertspec = tabHost.newTabSpec("Desserts");
    dessertspec.setIndicator(makeTabIndicator( getResources().getDrawable(R.drawable.cook_icon_tab_snacks)));
    Intent dessertsIntent = new Intent(this, Cook_tab_desserts.class);
    dessertspec.setContent(dessertsIntent);


    // Adding all TabSpec to TabHost
    tabHost.addTab(snackspec); // Adding snacks tab
    tabHost.addTab(mainspec); // Adding mains tab
    tabHost.addTab(dessertspec); // Adding desserts tab
}


//making the tab view:
private View makeTabIndicator(Drawable drawable){
ImageView Tabimage = new ImageView(this);
LayoutParams LP = new           LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,1);
LP.setMargins(1, 0, 1, 0);
Tabimage.setLayoutParams(LP);
Tabimage.setImageDrawable(drawable);
Tabimage.setBackgroundResource(R.drawable.tabview);
return Tabimage;


}}

I dont know weather or not i need the cook_layout anymore i'll see if i can remove it or leave it later... right now i just want to get it all working and later i'll come round by for a clean and tiding up hope that helps you guys out there that stumble upon this question! cheers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!