SlidingTabLayout底部选项卡水平居中(系列2)

找到Android SlidingTabLayout源代码,在Android SlidingTabLayout源代码中有一个方法:

private void populateTabStrip();


这是谷歌官方实现的Android SlidingTabLayout添加底部选项卡Tab的代码,如果为了实现前文所述的将Tab均分水平位置空间,则需要修改此方法,在此方法中添加如下代码:

LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.0f);
			tabView.setLayoutParams(layoutParams);

最终将private void populateTabStrip()改进成这样:
private void populateTabStrip() {
		final PagerAdapter adapter = mViewPager.getAdapter();
		final View.OnClickListener tabClickListener = new TabClickListener();

		for (int i = 0; i < adapter.getCount(); i++) {
			View tabView = null;
			TextView tabTitleView = 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;
			}

			tabTitleView.setText(adapter.getPageTitle(i));
			tabView.setOnClickListener(tabClickListener);

			//添加by Zhang Phil
			LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.0f);
			tabView.setLayoutParams(layoutParams);
			//添加by Zhang Phil
			
			mTabStrip.addView(tabView);
		}
	}


阅读更多
个人分类: android
上一篇滑动选项导航卡SlidingTabLayout和SlidingTabStrip(系列1)
下一篇Fragment
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭