您的位置:首页 > 其它

Actionbar在viewpager中使用的标题效果

2014-02-11 22:18 239 查看
活动:

/**
 * actionbar
 */
public class MainActivity extends FragmentActivity {
	private ViewPager mViewPager;
	private ActionBar mActionBar;
	private MyPagerAdapter mPagerAdapter;
	private List<String> titlelist = new ArrayList<String>();
	private List<Tab> tablist = new ArrayList<ActionBar.Tab>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	}

	public void initView() {
		titlelist.add("first");
		titlelist.add("second");
		titlelist.add("third");
		titlelist.add("fourth");
		titlelist.add("fifth");

		mViewPager = (ViewPager) findViewById(R.id.vp_test);
		mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(),
				titlelist);
		mViewPager.setAdapter(mPagerAdapter);
		mViewPager.setOnPageChangeListener(mPageChangeListener);
		// actionbar
		mActionBar = getActionBar();
		mActionBar.setNavigationMode(ActionBar.N***IGATION_MODE_TABS);
		mActionBar.setDisplayShowTitleEnabled(false);
		mActionBar.setDisplayShowHomeEnabled(false);

		for (int i = 0; i != titlelist.size(); i++) {
			tablist.add(mActionBar.newTab().setText(titlelist.get(i))
					.setTabListener(mTabListener));
			mActionBar.addTab(tablist.get(i));
		}

	}

	private OnPageChangeListener mPageChangeListener = new OnPageChangeListener() {

		@Override
		public void onPageSelected(int arg0) {
			mActionBar.setSelectedNavigationItem(arg0);
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}
	};

	private TabListener mTabListener = new TabListener() {

		@Override
		public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
			if (tab == tablist.get(0)) {
				mViewPager.setCurrentItem(0);
			} else if (tab == tablist.get(1)) {
				mViewPager.setCurrentItem(1);
			} else if (tab == tablist.get(2)) {
				mViewPager.setCurrentItem(2);
			}

		}

		@Override
		public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
			// TODO Auto-generated method stub

		}
	};
}


适配器:

public class MyPagerAdapter extends FragmentPagerAdapter {

	private List<String> titlelist;

	public MyPagerAdapter(FragmentManager fm, List<String> titlelist) {
		super(fm);
		// TODO Auto-generated constructor stub
		this.titlelist = titlelist;
	}

	@Override
	public Fragment getItem(int position) {
		// TODO Auto-generated method stub
		return MyFragment.create(titlelist.get(position));
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return titlelist.size();
	}

	public static class MyFragment extends Fragment {
		public static MyFragment create(String address) {
			MyFragment f = new MyFragment();
			Bundle b = new Bundle();
			b.putString("address", address);
			f.setArguments(b);
			return f;
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			Random r = new Random(System.currentTimeMillis());
			Bundle b = getArguments();
			View v = inflater.inflate(R.layout.fram, null);
			v.setBackgroundColor(r.nextInt() >> 8 | 0xFF << 24);
			return v;
		}

	}
}


main xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.view.ViewPager
        android:id="@+id/vp_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>


fragement xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="this is a fram"
        android:textSize="30sp" />

</LinearLayout>


效果:





注意:

1.改变actionbar的背景颜色:

<style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar">
        <item name="android:windowActionBarOverlay">true</item>
        <item name="android:height">50dp</item>
        <item name="android:background">#695cca</item>
        <!-- title背景 -->
        <item name="android:backgroundStacked">#06031f</item>
        <item name="android:backgroundSplit">#dc8508</item>
    </style>

    <style name="MyActionBarTabBarStyle" parent="@android:style/Widget.Holo.ActionBar.TabBar">
        <item name="android:divider">?android:attr/actionBarDivider</item>
        <item name="android:showDividers">none</item>
    </style>

    <style name="MyActionBarTabStyle" parent="@android:style/Widget.Holo.ActionBar.TabView">
        <item name="android:paddingTop">10dp</item>
        <!-- 整个tab的背景 -->>
        <!--  <item name="android:background">#680749</item>-->
    </style>

    <style name="Widget.Holo.ActionBar.TabText" parent="@android:style/Widget.ActionBar.TabText">
        <!-- 字体颜色 -->>
        <item name="android:textColor">@android:color/holo_green_dark</item>
        <item name="android:textSize">15sp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textAllCaps">true</item>
        <item name="android:ellipsize">marquee</item>
        <item name="android:maxLines">1</item>
    </style>


在主题中引用:

<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:windowAnimationStyle">@style/AnimationAc</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="android:actionBarTabBarStyle">@style/MyActionBarTabBarStyle</item>
        <item name="android:actionBarTabStyle">@style/MyActionBarTabStyle</item>
        <item name="android:actionBarTabTextStyle">@style/Widget.Holo.ActionBar.TabText</item>
    </style>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: