标签:
布局文件:来个铺满的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"
android:background="#33ffffff"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewPage"/>
</RelativeLayout>
java文件:为ViewPager加个适配器:
int imgIds[]={R.drawable.yindao21,R.drawable.yindao22,R.drawable.yindao23,R.drawable.yindao24};
SharedPreferences openTimes=getSharedPreferences("openTimes",0);
if(openTimes.getInt("times",0)==1){
startActivity(new Intent().setClass(getApplicationContext(),Index.class));
/**
* 从主页返回后直接退出
*/
finish();
}
ViewPager viewPager.setAdapter(new PagerAdapter() {
@Override
public Object instantiateItem(ViewGroup container, int position) {
LinearLayout linearLayout=new LinearLayout(getApplicationContext());
Button button=new Button(getApplicationContext());
button.setText("点击进入");
button.setBackgroundColor(Color.parseColor("#6b54f36b"));
/**
* 设置button的下边距
*/
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
lp.setMargins(22,22,22,22);
button.setLayoutParams(lp);
linearLayout.setBackgroundResource(imgIds[position]);
linearLayout.setGravity(Gravity.CENTER|Gravity.BOTTOM);
/**
* 如果是第4张引导图 在中下方加个按钮
*/
if(position==3) {
linearLayout.addView(button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent().setClass(getApplicationContext(),Index.class));
finish();
}
});
}
container.addView(linearLayout);
return linearLayout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
}
@Override
public int getCount() {
return imgIds.length;
}
@Override
public boolean isViewFromObject(View view, Object o) {
return view==o;
}
});
标签:
原文地址:http://www.cnblogs.com/jetereting/p/4454362.html