如何在标签中添加徽章?我正在使用此代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
Run Code Online (Sandbox Code Playgroud)
main activity.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
标签适配器java
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new RandomsFragment();
case 1:
return new ChatsFragment();
}
return null;
}
@Override
public int getCount() {
return 2;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在关注这个教程:http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/
我不明白如何在标签上添加徽章.
第一步是为每个标签创建自定义布局,以便:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
/>
Run Code Online (Sandbox Code Playgroud)
然后,当您想要向操作栏添加标签时,您必须执行以下操作:
private void addTabs(ActionBar actionBar)
{
ActionBar.Tab tab1=actionBar.newTab();
tab1.setTabListener(this);
tab1.setCustomView(R.layout.tab);
TextView txt1 = (TextView)tab1.getCustomView().findViewById(R.id.text1);
txt1.setText("Tab 1");
ActionBar.Tab tab2=actionBar.newTab();
tab2.setTabListener(this);
tab2.setCustomView(R.layout.tab);
TextView txt2 = (TextView)tab2.getCustomView().findViewById(R.id.text1);
txt2.setText("Tab 2");
ActionBar.Tab tab3=actionBar.newTab();
tab3.setCustomView(R.layout.tab);
tab3.setTabListener(this);
TextView txt3 = (TextView)tab3.getCustomView().findViewById(R.id.text1);
txt3.setText("Tab 3");
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
}
Run Code Online (Sandbox Code Playgroud)
所以为了访问它们并添加badgeView:
View v = getActionBar().getTabAt(0).getCustomView();
BadgeView badge = new BadgeView(getActivity(), v);
badge.setText("1");
badge.show();
Run Code Online (Sandbox Code Playgroud)
结果将是:
| 归档时间: |
|
| 查看次数: |
6041 次 |
| 最近记录: |