- <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=".MainActivity" >
- <!-- 用帧布局来形成一种叠加的效果 -->
- <FrameLayout
- android:layout_height="300dp"
- android:layout_width="match_parent"
- >
- <android.support.v4.view.ViewPager
- android:layout_height="match_parent"
- android:layout_width="match_parent"
- android:id="@+id/pager"
- android:background="#00ff00"
- />
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="70dp"
- android:layout_gravity="bottom"
- android:orientation="vertical"
- android:background="#33000000"
- >
- <LinearLayout
- android:layout_height="60dp"
- android:layout_width="match_parent"
- android:orientation="vertical"
- android:gravity="center_horizontal"
- >
- <TextView
- android:layout_height="match_parent"
- android:layout_width="wrap_content"
- android:textSize="20sp"
- android:gravity="center_horizontal"
- android:layout_marginTop="15dp"
- android:id="@+id/text"
- />
- </LinearLayout>
- <RelativeLayout
- android:layout_height="10dp"
- android:layout_width="match_parent"
- >
- <View
- android:layout_height="5dp"
- android:layout_width="5dp"
- android:background="@drawable/normal_focus"
- android:layout_centerHorizontal="true"
- android:id="@+id/v3"
- />
- <View
- android:layout_height="5dp"
- android:layout_width="5dp"
- android:background="@drawable/normal_focus"
- android:id="@+id/v2"
- android:layout_toLeftOf="@id/v3"
- android:layout_marginRight="5dp"
- />
- <View
- android:id="@+id/v1"
- android:layout_height="5dp"
- android:layout_width="5dp"
- android:background="@drawable/normal_focus"
- android:layout_toLeftOf="@id/v2"
- android:layout_marginRight="5dp"
- />
- <View
- android:layout_height="5dp"
- android:layout_width="5dp"
- android:background="@drawable/normal_focus"
- android:id="@+id/v4"
- android:layout_toRightOf="@id/v3"
- android:layout_marginLeft="5dp"
- />
- <View
- android:layout_height="5dp"
- android:layout_width="5dp"
- android:background="@drawable/normal_focus"
- android:id="@+id/v5"
- android:layout_toRightOf="@id/v4"
- android:layout_marginLeft="5dp"
- />
- </RelativeLayout>
- </LinearLayout>
- </FrameLayout>
- </RelativeLayout>
使用自定以的xml文件作为Drawable背景,来实现两种不同的”点“的样式。
dot_focus.xml:被选中时显示的点样式
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android"
- android:shape="oval"
- >
- <corners android:radius="5dp"/>
- <solid android:color="#ff000000"/>
- </shape>
normal_focus.xml:位选中的点样式:
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android"
- android:shape="oval"
- >
- <corners android:radius="5dp"/>
- <solid android:color="#ffff0000"/>
- </shape>
- public class MainActivity extends Activity {
- private ArrayList<ImageView>images;
- private ViewPager pager;
- private PagerAdapter adapter;
- private int oldIndex;
- private String[] titles;
- private TextView title;
- private View v1,v2,v3,v4,v5;
- private ArrayList<View>imageView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- v1=(View)findViewById(R.id.v1);
- v2=(View)findViewById(R.id.v2);
- v3=(View)findViewById(R.id.v3);
- v4=(View)findViewById(R.id.v4);
- v5=(View)findViewById(R.id.v5);
- images=new ArrayList<ImageView>();
- imageView=new ArrayList<View>();
- imageView.add(v1);
- imageView.add(v2);
- imageView.add(v3);
- imageView.add(v4);
- imageView.add(v5);
- int[]imageid=new int[]{R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};
- for(int i=0;i<5;i++)
- {
- ImageView image=new ImageView(this);
- image.setBackgroundResource(imageid[i]);
- images.add(image);
- }
- //下面的代码会报错。。。。原因是什么呢?
- /*ImageView image=new ImageView(this);
- image.setBackgroundResource(R.drawable.a);
- images.add(image);
- image.setBackgroundResource(R.drawable.b);
- images.add(image);
- image.setBackgroundResource(R.drawable.c);
- images.add(image);
- image.setBackgroundResource(R.drawable.d);
- images.add(image);
- image.setBackgroundResource(R.drawable.e);
- images.add(image);*/
- titles = new String[]{
- "巩俐不低俗,我就不能低俗",
- "扑树又回来啦!再唱经典老歌引万人大合唱",
- "揭秘北京电影如何升级",
- "乐视网TV版大派送",
- "热血屌丝的反杀"
- };
- pager=(ViewPager)findViewById(R.id.pager);
- title=(TextView)findViewById(R.id.text);
- //默认显示第一张图片和第一个标题,以一个点也使用不同的背景
- title.setText(titles[0]);
- v1.setBackgroundResource(R.drawable.dot_focus);
- oldIndex=0;
- adapter=new PagerAdapter()
- {
- @Override
- public int getCount() {
- return images.size();
- }
- //用于判断当前viewpager所显示的图片和要加载的图片是不是同一张图片
- @Override
- public boolean isViewFromObject(View arg0, Object arg1) {
- return arg0==arg1;
- }
- @Override
- public void destroyItem(ViewGroup container, int position,
- Object object) {
- //将视图从pager里移除。PagerAdapter默认在内存中最多加载三张图片
- container.removeView(images.get(position));
- //super.destroyItem(container, position, object);
- }
- @Override
- public Object instantiateItem(ViewGroup container, int position) {
- container.addView(images.get(position));
- return images.get(position);
- }
- };
- pager.setAdapter(adapter);
- pager.setOnPageChangeListener(new OnPageChangeListener(){
- @Override
- public void onPageScrollStateChanged(int arg0) {
- }
- @Override
- public void onPageScrolled(int arg0, float arg1, int arg2) {
- }
- @Override
- public void onPageSelected(int arg0) {
- //实现图片切换时图片和标点也随着改变的关键关键
- title.setText(titles[arg0]);
- imageView.get(arg0).setBackgroundResource(R.drawable.dot_focus);
- imageView.get(oldIndex).setBackgroundResource(R.drawable.normal_focus);
- oldIndex=arg0;
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- }