安卓启动页和引导页的制作

时间:2023-02-02 16:46:27
1,先在布局中设置启动页的图片
<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="@drawable/splash_bg_newyear"
tools:context=".MainActivity" >

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/splash_horse_newyear"
android:scaleType="fitXY" />

</RelativeLayout>
2,然后在布局中设置启动页,启动页其实就是一个viewPager,但是这里的viewPager必须是v4包下的,因为v4包是向下兼容的,如果版本低的安卓手机也同样适用
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <android.support.v4.view.ViewPager        android:id="@+id/view_pager"        android:layout_width="match_parent"        android:layout_height="match_parent" />    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true"        android:layout_marginBottom="30dp"        android:paddingLeft="30dp"        android:paddingRight="30dp"        android:text="@string/kaishi"        android:textSize="20sp"        android:visibility="gone" /></RelativeLayout>
3,进入主界面,设置主界面的布局

这个主界面的布局任意,你想加载什么样的效果就设置什么样的布局,这里就不赘述了,任意一个布局都OK,我在这里用的是一个关于RadioGroup的布局

<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" >

<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/div_view" >
</FrameLayout>

<View
android:id="@+id/div_view"
android:layout_width="fill_parent"
android:layout_height="1.0px"
android:layout_above="@+id/main_radiogroup"
android:layout_marginBottom="2dip"
android:background="#ffc9cacb" />

<RadioGroup
android:id="@+id/main_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:background="@drawable/white" >

<RadioButton
android:id="@+id/tab_rbo_question"
style="@style/tab_textview"
android:checked="true"
android:drawableTop="@drawable/selector_tab_question"
android:text="问他"
android:textColor="@color/tv_checked_bg" />

<RadioButton
android:id="@+id/tab_rbo_message"
style="@style/tab_textview"
android:drawableTop="@drawable/selector_tab_message"
android:text="消息" />

<RadioButton
android:id="@+id/tab_rbo_answer"
style="@style/tab_textview"
android:drawableTop="@drawable/selector_tab_answer"
android:text="我要回答" />

<RadioButton
android:id="@+id/tab_rbo_discover"
style="@style/tab_textview"
android:drawableTop="@drawable/selector_tab_discover"
android:text="发现" />

<RadioButton
android:id="@+id/tab_rbo_user"
style="@style/tab_textview"
android:drawableTop="@drawable/selector_tab_user"
android:text="我" />
</RadioGroup>

</RelativeLayout>


4,在java代码中写逻辑

(1)

MainActivity中:启动页的逻辑,因为启动页打开是一个耗时操作,所以使用handler进行处理,避免anr的出现,主线程可以对UI进行修改

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
new Handler() {
public void handleMessage(android.os.Message msg) {
SharedPreferences sp = getSharedPreferences("config", 0);
boolean b = sp.getBoolean("is_frist", true);
if (b) {
Editor edit = sp.edit();
edit.putBoolean("is_frist", false);
edit.commit();
Intent intent = new Intent(getApplicationContext(),
GuideActivity.class);
startActivity(intent);

} else {
Intent intent = new Intent(getApplicationContext(),
HomeActivity.class);
startActivity(intent);

}
finish();

};
}.sendEmptyMessageDelayed(1, 3000);
}

}

(2)GuideActivity中,引导页的逻辑,需要一个适配器

public class GuideActivity extends Activity {
private ArrayList<ImageView> list;
private Button button;
private ViewPager view_pager;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_guide);
list = new ArrayList<ImageView>();
ImageView img1 = new ImageView(getApplicationContext());
img1.setBackgroundResource(R.drawable.guide_1);
ImageView img2 = new ImageView(getApplicationContext());
img2.setBackgroundResource(R.drawable.guide_2);
ImageView img3 = new ImageView(getApplicationContext());
img3.setBackgroundResource(R.drawable.guide_3);
list.add(img1);
list.add(img2);
list.add(img3);
button = (Button) findViewById(R.id.button);
view_pager = (ViewPager) findViewById(R.id.view_pager);
view_pager.setAdapter(new MyPagerAdapter(list));
view_pager.setOnPageChangeListener(new OnPageChangeListener() {

@Override
public void onPageSelected(int arg0) {
if(arg0==list.size()-1){
button.setVisibility(View.VISIBLE);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
startActivity(new Intent(GuideActivity.this,HomeActivity.class));
finish();
}
});
}else{
button.setVisibility(View.GONE);
}
}

@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub

}
});

}

}

适配器的逻辑

public class MyPagerAdapter extends PagerAdapter {

private ArrayList<ImageView> al = new ArrayList<ImageView>();
public MyPagerAdapter(ArrayList<ImageView> list) {
this.al = list;
}

@Override
public int getCount() {
return al.size();
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0==arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(al.get(position));
return al.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}

}

(3)主页中,这是一个关于RadioGroup的应用

public class HomeActivity extends FragmentActivity {
private RadioGroup main_radiogroup;
private FragmentTransaction ft;
private FragmentManager fm;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_home);
main_radiogroup = (RadioGroup) this.findViewById(R.id.main_radiogroup);
fm = this.getSupportFragmentManager();
main_radiogroup
.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int length = group.getChildCount();
for (int i = 0; i < length; i++) {
RadioButton rgo = (RadioButton) group.getChildAt(i);
if (rgo.getId() != checkedId)
rgo.setTextColor(getResources().getColor(
R.color.tv_checked_nor));
else
rgo.setTextColor(getResources().getColor(
R.color.tv_checked_bg));
}

switch (checkedId) {
case R.id.tab_rbo_answer:
change(new Fragment04());
break;
case R.id.tab_rbo_discover:
change(new Fragment02());
break;
case R.id.tab_rbo_message:
change(new Fragment03());
break;
case R.id.tab_rbo_question:
change(new Fragment01());
break;
case R.id.tab_rbo_user:
change(new Fragment05());
break;
}
}
});
change(new Fragment01());
}
private void change(Fragment f) {
ft = fm.beginTransaction();
ft.replace(R.id.frame_container, f);
ft.commit();
}

}

最后别忘记在清单文件中注册Activity