使用Tablayout进行徽章通知

时间:2017-04-22 08:55:37

标签: android

这是我的标签布局:

enter image description here

有人会帮我在每个Tablyout图标上添加徽章通知吗?

我在这里搜索了折旧的解决方案。

这是我的活动课程:

     public class navigation extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
      private TabLayout tabLayout;
      private InterstitialAd mInterstitialAd;
      private Button mNextLevelButton;
      private TextView mTextView;
      private ViewPager viewPager;
      private int[] tabIcons = {
            R.drawable.ic_favorite_final,
            R.drawable.ic_live_final,
            R.drawable.ic_matches_final,
            R.drawable.ic_sort
            };


       @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation);
        MobileAds.initialize(getApplicationContext(), "ca-app-pub-
         7294884496094914~4747804788");
        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);


        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT ) != 
        0) {
            // Activity was brought to front and not created,
            // Thus finishing this will get us to the last viewed activity
            finish();
            return;
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        setupTabIcons();

    }

    private void setupTabIcons() {
        tabLayout.getTabAt(0).setIcon(tabIcons[0]);
        tabLayout.getTabAt(1).setIcon(tabIcons[1]);
        tabLayout.getTabAt(2).setIcon(tabIcons[2]);
     //   tabLayout.getTabAt(3).setIcon(tabIcons[3]);

    }

    private void setupViewPager(ViewPager viewPager) {
       ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new OneFragment());
     //   adapter.addFrag(new TwoFragment(), getString(R.string.livestream));
        adapter.addFrag(new ThreeFragment());
        adapter.addFrag(new FourFragment());
        viewPager.setAdapter(adapter);
    }
    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFrag(Fragment fragment) {
            mFragmentList.add(fragment);
           // mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
           // return mFragmentTitleList.get(position);
            return null;
        }
    }
  }

然后我想在标题图标图像上添加总计数通知(如果可能)

更新:

  <?xml version="1.0" encoding="utf-8"?>
  <android.support.design.widget.CoordinatorLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tiger.alahedclub.activity.navigation">



<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:popupTheme="@style/generalnotitle"
 >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        app:titleTextColor="@android:color/black"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/yellow"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/generalnotitle">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">


        <TextView
            android:id="@+id/action_bar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_centerHorizontal="true"
            android:text="@string/app_name"
            android:textColor="@android:color/black"
            android:textSize="24sp" />
        <ImageView
            android:background="@drawable/ahed_small"
            android:id="@+id/imagetitle"
            android:layout_width="40dp"
            android:layout_height="25dp"
            android:layout_centerVertical="true"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:layout_toStartOf="@+id/action_bar_title"
            android:layout_toLeftOf="@+id/action_bar_title"/>

        </RelativeLayout>
    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:background="@color/yellow"
        app:tabTextColor="@android:color/black"
        app:tabSelectedTextColor="@android:color/black"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabIndicatorHeight="4dp"
        app:tabMode="fixed"
        app:tabGravity="fill"  />

</android.support.design.widget.AppBarLayout>

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    ads:adSize="SMART_BANNER"
    android:layout_gravity="bottom"
    ads:adUnitId="@string/banner_ad_unit_id">

</com.google.android.gms.ads.AdView>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/cardview_back"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
   </android.support.design.widget.CoordinatorLayout>

更新2: enter image description here

3 个答案:

答案 0 :(得分:1)

 tabLayout.getTabAt(0).setIcon(buildCounterDrawable(COUNT));

buildCounterDrawable方法,

 private Drawable buildCounterDrawable(int count) {
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.cart_overflow, null);
        view.setBackgroundResource(R.drawable.ic_tool_cart);

        TextView textView = (TextView) view.findViewById(R.id.count);

        if (count == 0) {
            textView.setVisibility(View.GONE);
        } else {
            textView.setVisibility(View.VISIBLE);
            textView.setText(String.valueOf(count));
        }

        view.measure(
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

        view.setDrawingCacheEnabled(true);
        view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(false);

        return new BitmapDrawable(getResources(), bitmap);
    }

cart_overflow.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/counterPanel"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:background="@drawable/ic_tool_cart"> <!-- Notication Icon -->

        <TextView
            android:layout_gravity="top|end"
            android:gravity="center"
            android:background="@drawable/ic_cart_count"
            android:id="@+id/count"
            android:layout_width="18dp"
            android:layout_height="18dp"
            android:textSize="12sp"
            android:textColor="#FFFFFF"/>

</FrameLayout>

drawable - ic_cart_count(计数器背景)

 <vector
    android:height="18dp"
    android:viewportHeight="24.0"
    android:viewportWidth="24.0"
    android:width="18dp"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <path android:fillColor="#FFEF0777"
        android:pathData="M12,12m-10,0a10,10 0,1 1,20 0a10,10 0,1 1,-20 0"/>

</vector>

我为一张图片做了这个。您可以通过添加布局和图像作为参数来根据需要进行更改。

答案 1 :(得分:1)

 private Drawable buildCounterDrawable(int count, int backgroundImageId) {
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.noitification_count, null);
        view.setBackgroundResource(backgroundImageId);

        if (count == 0) {
            View counterTextPanel = view.findViewById(R.id.badge_layout1);
            counterTextPanel.setVisibility(View.GONE);
        } else {
            TextView textView = (TextView) view.findViewById(R.id.badge_notification_1);
            textView.setText("" + count);
        }

        view.measure(
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

        view.setDrawingCacheEnabled(true);
        view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(false);

        return new BitmapDrawable(getResources(), bitmap);
    }

和setIcon作为此视图。

 tabLayout.getTabAt(0).setIcon(buildCounterDrawable(1,R.drawable.ic_count_bg));

尝试这对我有用..

答案 2 :(得分:0)

试试这个。 onCreateOptionMenu的代码。

 @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            super.onCreateOptionsMenu(menu);
            getMenuInflater().inflate(R.menu.menu_main, menu);

            MenuItem menuItem = menu.findItem(R.id.action_settings);
            menuItem.setIcon(buildCounterDrawable(4, android.R.color.transparent));


            return true;
        }

        private Drawable buildCounterDrawable(int count, int backgroundImageId) {
            LayoutInflater inflater = LayoutInflater.from(this);
            View view = inflater.inflate(R.layout.noitification_count, null);
            view.setBackgroundResource(backgroundImageId);

            if (count == 0) {
                View counterTextPanel = view.findViewById(R.id.badge_layout1);
                counterTextPanel.setVisibility(View.GONE);
            } else {
                TextView textView = (TextView) view.findViewById(R.id.badge_notification_1);
                textView.setText("" + count);
            }

            view.measure(
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

            view.setDrawingCacheEnabled(true);
            view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
            Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
            view.setDrawingCacheEnabled(false);

            return new BitmapDrawable(getResources(), bitmap);
        }

notification_count.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@android:style/Widget.ActionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:clickable="true"
    android:gravity="center"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/badge_layout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:id="@+id/relative_layout_item_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <Button
                android:id="@+id/button1"
                android:layout_width="@dimen/_100sdp"
                android:layout_height="@dimen/_100sdp"
                android:background="@drawable/ic_dollar" />
        </RelativeLayout>

        <TextView
            android:id="@+id/badge_notification_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@id/relative_layout_item_count"
            android:background="@drawable/circle_bg"
            android:gravity="center"
            android:text="12"
            android:textColor="#FFF"
            android:padding="@dimen/_10sdp"
            android:textSize="@dimen/_30sdp"
            android:textStyle="bold" />
    </RelativeLayout>
        </RelativeLayout>

放置你的形状文件

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid
        android:color="@color/green"/>
</shape>