即使启用后,Tab Strip也会禁用

时间:2014-06-20 09:00:20

标签: android xml android-tabhost

我的tabhost存在问题,无法启用Tabstrip。甚至认为我也尝试在XML和Java代码中启用。以下是我的Java和XML代码。

public class BoardsTabHost extends TabActivity implements OnTabChangeListener {

private static final String[] TABS = { "BoardsStatsGroupActivity",
        "BoardsBidsGroupActivity", "BoardsCreativeGroupActivity" };
private static final String[] TAB_NAMES = { "Stats", "Bids", "Creative" };
public static TabHost tabs;
public static TabWidget tabWidget;
protected Bitmap roundedImage;
public boolean checkTabsListener = false;
ImageButton previousPage, deleteIcon;
Intent intent;
TextView boardsTabhostTitle;
Typeface typeFace1, typeFace2, typeFace3, myriaproReg;

int showtab = 0;

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.boards_tabhost);
    /**** adding animation ****/
    this.overridePendingTransition(R.anim.animation_leave,
            R.anim.animation_enter);
    // Setting Functionality for back button for campaign tab host
    previousPage = (ImageButton) findViewById(R.id.previousPageImageButton);
    deleteIcon = (ImageButton) findViewById(R.id.deleteImageButtonBoards);

    boardsTabhostTitle = (TextView) findViewById(R.id.boardsTabhostTitle);


    typeFace1 = Typeface.createFromAsset(getAssets(), "Lato-Bol.ttf");
    typeFace2 = Typeface.createFromAsset(getAssets(), "Lato-Reg.ttf");
    typeFace3 = Typeface.createFromAsset(getAssets(), "Lato-Bla.ttf");
    myriaproReg = Typeface.createFromAsset(getAssets(),
            "MYRIADPRO-REGULAR.OTF");

    boardsTabhostTitle.setTypeface(myriaproReg);

    /*** create intent instance ****/
    intent = getIntent();
    showtab = intent.getIntExtra("select", 0);
    intent = new Intent();
    deleteIcon.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            intent.setClass(getApplicationContext(),
                    ConfirmationModalDelete.class);
            startActivity(intent);

        }
    });

    previousPage.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            intent.setClass(getApplicationContext(),                       TabHostActivity.class);
            intent.putExtra("select", 2);
            startActivity(intent);
        }
    });

    tabs = getTabHost();
    tabWidget = tabs.getTabWidget();
    // Tab strip Enabled here
    tabs.getTabWidget().setStripEnabled(true);
    tabs.getTabWidget().setLeftStripDrawable(R.drawable.my_strip_tab);
    tabs.getTabWidget().setLeftStripDrawable(R.drawable.my_strip_tab);

    tabs.setOnTabChangedListener(this);

    for (int i = 0; i < TABS.length; i++) {
        TabHost.TabSpec tab = tabs.newTabSpec(TABS[i]);

        // Associating Components
        ComponentName oneActivity = new ComponentName(
                "com.igreens.fliphound", "com.igreens.fliphound." + TABS[i]);
        Intent intent = new Intent().setComponent(oneActivity);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        tab.setContent(intent);
        // Setting the Indicator
        MyTabIndicator myTab = new MyTabIndicator(this, TAB_NAMES[i],
                (i + 1), null);
        tab.setIndicator(myTab);
        tabs.addTab(tab);
    }

    checkTabsListener = true;

    for (int i = 0; i < tabs.getTabWidget().getChildCount(); i++) {
        tabs.getTabWidget().getChildAt(i)
                .setBackgroundColor(Color.parseColor("#1D1D1F"));

    }
    tabs.getTabWidget().getChildAt(0)
            .setBackgroundColor(Color.parseColor("#1D1D1F"));

    // Maintaining Clicks
    // Stats Tab Click

    tabs.setCurrentTab(showtab);
    tabWidget.getChildAt(0).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (BoardsStatsGroupActivity.boardsStatsGroupStack != null
                    && BoardsStatsGroupActivity.boardsStatsGroupStack.mIdList
                            .size() > 1) {
                BoardsStatsGroupActivity.boardsStatsGroupStack
                        .getLocalActivityManager().removeAllActivities();
                BoardsStatsGroupActivity.boardsStatsGroupStack.mIdList
                        .clear();
                BoardsStatsGroupActivity.boardsStatsGroupStack.mIntents
                        .clear();
                BoardsStatsGroupActivity.boardsStatsGroupStack.mAnimator
                        .removeAllViews();
                BoardsStatsGroupActivity.boardsStatsGroupStack
                        .startChildActivity(
                                "BoardsStatsActivity",
                                new Intent(
                                        BoardsStatsGroupActivity.boardsStatsGroupStack,
                                        BoardsStatsActivity.class));

            }

            tabWidget.setCurrentTab(0);
            tabs.setCurrentTab(0);
        }
    });

    // Edit tab click

    tabWidget.getChildAt(1).setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (BoardsBidsGroupActivity.boardsBidsGroupStack != null
                    && BoardsBidsGroupActivity.boardsBidsGroupStack.mIdList
                            .size() > 0) {

                BoardsBidsGroupActivity.boardsBidsGroupStack
                        .getLocalActivityManager().removeAllActivities();
                BoardsBidsGroupActivity.boardsBidsGroupStack.mIdList
                        .clear();
                BoardsBidsGroupActivity.boardsBidsGroupStack.mIntents
                        .clear();
                BoardsBidsGroupActivity.boardsBidsGroupStack.mAnimator
                        .removeAllViews();
                BoardsBidsGroupActivity.boardsBidsGroupStack
                        .startChildActivity(
                                "BoardsBidsActivity",
                                new Intent(
                                        BoardsBidsGroupActivity.boardsBidsGroupStack,
                                        BoardsBidsActivity.class));
            }

            tabWidget.setCurrentTab(1);
            tabs.setCurrentTab(1);
        }
    });

    // Stats Tab Click
    tabWidget.getChildAt(2).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (BoardsCreativeGroupActivity.boardsCreativeGroupStack != null
                    && BoardsCreativeGroupActivity.boardsCreativeGroupStack.mIdList
                            .size() > 1) {
                BoardsCreativeGroupActivity.boardsCreativeGroupStack
                        .getLocalActivityManager().removeAllActivities();
                BoardsCreativeGroupActivity.boardsCreativeGroupStack.mIdList
                        .clear();
                BoardsCreativeGroupActivity.boardsCreativeGroupStack.mIntents
                        .clear();
                BoardsCreativeGroupActivity.boardsCreativeGroupStack.mAnimator
                        .removeAllViews();
                BoardsCreativeGroupActivity.boardsCreativeGroupStack
                        .startChildActivity(
                                "BoardsCreativeActivity",
                                new Intent(
                                        BoardsCreativeGroupActivity.boardsCreativeGroupStack,
                                        BoardsCreativeActivity.class));

            }

            tabWidget.setCurrentTab(2);
            tabs.setCurrentTab(2);
        }
    });
}

public class MyTabIndicator extends RelativeLayout {
    public MyTabIndicator(Context context, String label, int tabId,
            Bitmap bgImg) {
        super(context);
        RelativeLayout tab = null;
        TextView tv;
        this.setGravity(Gravity.CENTER);

        if (tabId == 1) {
            tab = (RelativeLayout) LayoutInflater.from(context).inflate(
                    R.layout.boards_group_stats, null);
            tv = (TextView) tab.findViewById(R.id.tab_label);
            tv.setTypeface(typeFace2);
            tv.setText(label);
        } else if (tabId == 2) {
            tab = (RelativeLayout) LayoutInflater.from(context).inflate(
                    R.layout.boards_group_bids, null);
            tv = (TextView) tab.findViewById(R.id.tab_label);
            tv.setTypeface(typeFace2);

            tv.setText(label);
        }

        else if (tabId == 3) {
            tab = (RelativeLayout) LayoutInflater.from(context).inflate(
                    R.layout.boards_group_creative, null);
            tv = (TextView) tab.findViewById(R.id.tab_label);
            tv.setTypeface(typeFace2);

            tv.setText(label);
        }

        this.addView(tab);
    }
}

public void onTabChanged(String tabId) {
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(tabs.getApplicationWindowToken(), 0);

    for (int i = 0; i < tabs.getTabWidget().getChildCount(); i++) {
        tabs.getTabWidget().getChildAt(i)
                .setBackgroundColor(Color.parseColor("#1D1D1F"));

    }
    tabs.getTabWidget().getChildAt(tabs.getCurrentTab())
            .setBackgroundColor(Color.parseColor("#1D1D1F"));

}

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,
        float roundPxRadius) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = roundPxRadius;

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

public void onResume() {
    super.onResume();
    // ReConstructing TabViews
    reDesignTabViews();
}

public void onPause() {
    super.onPause();
}

/**
 * Method used to re constructing the Views at tab bar. This solves tabs
 * disappearing issue.
 */
public void reDesignTabViews() {
    MyTabIndicator myIndicator;

    // Construction of tab views....
    for (int i = 0; i < tabWidget.getChildCount(); i++) {
        myIndicator = (MyTabIndicator) tabWidget.getChildAt(i);
        myIndicator.removeAllViews();

        switch (i) {

        case 0:
            myIndicator.addView((RelativeLayout) LayoutInflater.from(
                    getApplicationContext()).inflate(
                    R.layout.boards_group_stats, null));
            break;

        case 1:
            myIndicator.addView((RelativeLayout) LayoutInflater.from(
                    getApplicationContext()).inflate(
                    R.layout.boards_group_bids, null));
            break;
        case 2:
            myIndicator.addView((RelativeLayout) LayoutInflater.from(
                    getApplicationContext()).inflate(
                    R.layout.boards_group_creative, null));
            break;

        }
    }
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.animation_back_leave,
            R.anim.animation_back_enter);
}
}

以下是布局的XML文件。即使在XML文件中也启用Tabstrip的位置。但仍然无法正常工作

  <TabHost
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:foregroundGravity="center" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:showDividers="none"
            android:tabStripEnabled="true"
            android:tabStripLeft="@drawable/my_strip_tab"
            android:tabStripRight="@drawable/my_strip_tab" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</TabHost>  

0 个答案:

没有答案