ViewPager是Android SDk中自带的一个附加包android-support-v4.jar中的一个类,可以用来实现屏幕间的切换。android-support-v4.jar可以在网上搜索最新的版本,下载好它后,我们需要把它添加到项目中去。
首先来看一下activity的布局,这个布局相信大家都能看得懂,第一行为只有两个TextView的页标,至于名字大家就不用在意了,哈哈,第二行为滑动界面时的滚动条,图片自己要选择并添加到drawable中,长度不要太长哦,第三行即为我们要实现的界面切换用的ViewPager:
<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" tools:context=".MediaPlayerActivity"> <LinearLayout android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="50.0dip" android:background="#FFFFFF" > <!--layout_weight这个属性为权重,让两个textview平分这个linearLayout--> <TextView android:id="@+id/videoLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1.0" android:gravity="center" android:text="视频" android:textColor="#000000" android:textSize="20dip" android:background="@drawable/selector"/> <TextView android:id="@+id/musicLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1.0" android:gravity="center" android:text="音乐" android:textColor="#000000" android:textSize="20dip" android:background="@drawable/selector"/> </LinearLayout> <ImageView android:layout_width="match_parent" android:layout_height="10dp" android:layout_below="@id/linearLayout" android:id="@+id/scrollbar" android:scaleType="matrix" android:src="@drawable/scrollbar"/> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/scrollbar"> </android.support.v4.view.ViewPager></RelativeLayout>1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515212345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152布局中TextView的background属性是我先设置好的,可以实现在按压其时,可以使得其背景颜色得到变换,并在松开时恢复颜色。方法为在drawable中新建一个selector.xml文件,写下如下代码; selector.xml:
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_PRessed="true" android:drawable="@color/press" /></selector>123456123456当然,首先要在values文件夹下新建好colors.xml文件,配置好press的颜色: colors.xml:
<?xml version="1.0" encoding="utf-8"?><resources> <color name="press">#25fa55</color></resources>12341234看完了activity的布局,我们再来看看想要切换的界面的布局,这两个布局文件只需在layout文件中新建就好,不需要新建activity,为了简单,这里就只设置了背景颜色,能够在测试时看到效果即可: video_player.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ad2929"></RelativeLayout>123456123456media_player.xml:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#acbbcf"></RelativeLayout>123456123456新闻热点
疑难解答