activity_main.xml bottom_tab_text_color_selector.xml MainActivity.java package com.zhoan.hcym;import com.xinbo.utils.TextViewUtils;import com.zhoan.hcym.fragment.ExitDialogFragment;import android.os.Bundle;import android.os.Handler;import a
bottom_tab_text_color_selector.xml
MainActivity.java
package com.zhoan.hcym; import com.xinbo.utils.TextViewUtils; import com.zhoan.hcym.fragment.ExitDialogFragment; import android.os.Bundle; import android.os.Handler; import android.support.v4.app.FragmentTabHost; import android.support.v7.app.AppCompatActivity; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private FragmentTabHost mTabHost; private Handler mHandler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initTab(); } private void initTab() { mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); addTab("首页", R.drawable.selector_home, HomeFragment.class); addTab("分类", R.drawable.selector_catagory, CatagoryFragment.class); addTab("购物车", R.drawable.selector_shopcart, CartFragment.class); addTab("我的", R.drawable.selector_mine, MineFragment.class); } private void addTab(String title, int selector, Class class1) { addTab(title, selector, class1, null); } private void addTab(String title, int selector, Class class1, Bundle bundle) { View layout = getLayoutInflater().inflate(R.layout.tab, null); TextView tabTitle = (TextView) layout.findViewById(R.id.tab_title); tabTitle.setText(title); TextViewUtils.setTextDrawable(this, selector, tabTitle); mTabHost.addTab(mTabHost.newTabSpec(title).setIndicator(layout), class1, bundle); } /* * @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) { // Handle * action bar item clicks here. The action bar will // automatically handle * clicks on the Home/Up button, so long // as you specify a parent activity * in AndroidManifest.xml. int id = item.getItemId(); if (id == * R.id.action_settings) { return true; } return * super.onOptionsItemSelected(item); } */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { String currentTabTag = mTabHost.getCurrentTabTag(); if ("首页".equals(currentTabTag)) { mHandler.postDelayed(new Runnable() { public void run() { popupExitDialog(); } }, 2000); } else { mTabHost.setCurrentTabByTag("首页"); } return false; } return super.onKeyDown(keyCode, event); } public void popupExitDialog() { ExitDialogFragment exitDialogFragment = new ExitDialogFragment(); /* * exitDialogFragment.setOnDialogClickListener(new * OnDialogClickListener() { * * @Override public void OnDialogClick() { // TODO Auto-generated method * stub finish(); } }); */ exitDialogFragment.show(getFragmentManager(), null); } }selector_home.xml
tab.xml
TextViewUtils.java
package com.xinbo.utils; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.assist.FailReason; import com.nostra13.universalimageloader.core.listener.ImageLoadingListener; import android.content.Context; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.view.View; import android.widget.TextView; public final class TextViewUtils { public static void setTextDrawable(Context context, int drawableRes, TextView tvName) { Drawable drawableTop = context.getResources().getDrawable(drawableRes); // 必须设置图片大小,否则不显示 drawableTop.setBounds(0, 0, drawableTop.getMinimumWidth(), drawableTop.getMinimumHeight()); tvName.setCompoundDrawables(null, drawableTop, null, null); } public static void setTextDrawable(Context context, Bitmap bitmap, TextView tvName) { Drawable drawableTop = BitmapUtils.bitmap2Drawable(bitmap); // 必须设置图片大小,否则不显示 drawableTop.setBounds(0, 0, drawableTop.getMinimumWidth(), drawableTop.getMinimumHeight()); tvName.setCompoundDrawables(null, drawableTop, null, null); } public static void setTextDrawable(Context context, String url, final TextView textView) { ImageLoader.getInstance().loadImage(url, new ImageLoadingListener() { public void onLoadingStarted(String arg0, View arg1) { } public void onLoadingFailed(String arg0, View arg1, FailReason arg2) { } public void onLoadingComplete(String arg0, View arg1, Bitmap bitmap) { Drawable drawableTop = BitmapUtils.bitmap2Drawable(bitmap); // 必须设置图片大小,否则不显示 drawableTop.setBounds(0, 0, drawableTop.getMinimumWidth(), drawableTop.getMinimumHeight()); textView.setCompoundDrawables(null, drawableTop, null, null); } public void onLoadingCancelled(String arg0, View arg1) { } }); } }styles.xml