本文介绍了SlidingTabLayout带图标只的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
随着this比如我是只能够创建一个SlidingTabLayout的图标,但我想他们的中心。我是否需要创建一个自定义视图,还是有其他的解决方案?
Following this example i was able to create a SlidingTabLayout with icons only but i would like to center them. Do i need to create a custom view or are there other solutions?
任何帮助将是AP preciated。
Any help would be appreciated.
推荐答案
最后,我心中已经想通了。这是我的code例如...
Finally i'v figured it out. Here's my code example...
为每个标签创建一个可绘制的,在我的情况,我创建 store_tab.xml
和 cart_tab.xml
Create a drawable for each tab, in my case i createdstore_tab.xml
and cart_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_store_white_24dp" android:state_selected="true" />
<item android:drawable="@drawable/ic_store_grey600_24dp" />
</selector>
创建自己的传呼机适配器,它从 FragmentPageAdapter
public class HomePagerAdapter extends FragmentPagerAdapter {
private static int[] ICONS = new int[] {
R.drawable.store_tab,
R.drawable.cart_tab
};
// ...
@Override
public CharSequence getPageTitle(int position) {
return null;
}
@Override
public int getCount() {
return ICONS.length;
}
public int getDrawableId(int position) {
return ICONS[position];
}
}
在创建 SlidingTabLayout
protected ImageView createDefaultImageView(Context context) {
ImageView imageView = new ImageView(context);
int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
imageView.setPadding(padding, padding, padding, padding);
int width = (int) (getResources().getDisplayMetrics().widthPixels / mViewPager.getAdapter().getCount());
imageView.setMinimumWidth(width);
return imageView;
}
然后修改方法 populateTabStrip
在 SlidingTabLayout
private void populateTabStrip() {
final HomePagerAdapter adapter = (HomePagerAdapter) mViewPager.getAdapter();
final View.OnClickListener tabClickListener = new TabClickListener();
for (int i = 0; i < adapter.getCount(); i++) {
View tabView = null;
//TextView tabTitleView = null;
ImageView tabIconView = null;
/*if (mTabViewLayoutId != 0) {
// If there is a custom tab view layout id set, try and inflate it
tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip,
false);
tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
}
if (tabView == null) {
tabView = createDefaultTabView(getContext());
}
if (tabTitleView == null && TextView.class.isInstance(tabView)) {
tabTitleView = (TextView) tabView;
}*/
if (tabView == null) {
tabView = createDefaultImageView(getContext());
}
if (tabIconView == null && ImageView.class.isInstance(tabView)) {
tabIconView = (ImageView) tabView;
}
tabIconView.setImageDrawable(getResources().getDrawable(adapter.getDrawableId(i)));
if (mViewPager.getCurrentItem() == i) {
tabIconView.setSelected(true);
}
//tabTitleView.setText(adapter.getPageTitle(i));
tabView.setOnClickListener(tabClickListener);
mTabStrip.addView(tabView);
}
}
最后修改 onPageSelected
在 InternalViewPageListener
@Override
public void onPageSelected(int position) {
for (int i = 0; i < mTabStrip.getChildCount(); i++) {
mTabStrip.getChildAt(i).setSelected(false);
}
mTabStrip.getChildAt(position).setSelected(true);
if (mScrollState == ViewPager.SCROLL_STATE_IDLE) {
mTabStrip.onViewPagerPageChanged(position, 0f);
scrollToTab(position, 0);
}
if (mViewPagerPageChangeListener != null) {
mViewPagerPageChangeListener.onPageSelected(position);
}
}
最终结果
这篇关于SlidingTabLayout带图标只的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!