본문 바로가기

자료

탭 버튼에 badge 달기

 

  // 탭 설정
  tabHost = (TabHost) findViewById(R.id.tab_host);
  tabHost.setup(getLocalActivityManager());
  TabHost.TabSpec spec;

 

  CustomTabBadge mTabWidget1 = new CustomTabBadge(this);
  mTabWidget1.setImage(getResources().getDrawable(R.drawable.tab_photo_indicator));
  mTabWidget1.setCount(0);
  Intent intent1 = new Intent(this, PhotoMeetGroup.class);
  spec = tabHost.newTabSpec("메뉴").setIndicator(mTabWidget1).setContent(intent1);
  tabHost.addTab(spec); // Tab 등록

 

#########################################################

public class CustomTabBadge extends FrameLayout {
 Context mContext;
 TextView mTv; // 숫자 입력
 ImageView mIv;
 public CustomTabBadge(Context context) {
  super(context);
  mContext = context;
  init();
 }

 public CustomTabBadge(Context context, AttributeSet attrs) {
  super(context, attrs);
  mContext = context;
  init();
 }

 public CustomTabBadge(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  mContext = context;
  init();
 }

 public void init() {
  LayoutInflater.from(mContext).inflate(R.layout.custom_tab_badge, this, true);
  mIv = (ImageView) findViewById(R.id.iv);
  mTv = (TextView) findViewById(R.id.tv);
 }

 public void setCount(String str) {
  setCount("" + str);
 }

 public void setCount(int i) {
  if (i != 0) {
   mTv.setVisibility(View.VISIBLE);
   mTv.setText("" + i);
  } else {
   mTv.setVisibility(View.GONE);
  }
 }

 public void setImage(Drawable d) {
  mIv.setImageDrawable(d);
 }

}

 

############################################

 

badge를 표시할 xml

custom_tab_badge.xml

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/iv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:scaleType="fitXY" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="top|center_horizontal" >

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dip"
            android:background="@drawable/icon_new"
            android:gravity="center"
            android:singleLine="true"
            android:textColor="@android:color/black"
            android:textSize="1dp" />
    </LinearLayout>

</RelativeLayout>

 

이 외에는 일반 tab 사용과 동일

 

 

응용/참조 : http://www.kiwook.pe.kr/tag/Badge