先上效果图
页面很简单,下面是三个RadioButton,当点击他们的时候上方的页面改变,上方是一个容器,用来盛放不同的页面。
接下来看看代码是如何实现这个功能的:
主界面main.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" >
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical"
android:layout_weight="1.0">
</LinearLayout>
<RadioGroup
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/btn_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:button="@null"
android:background="@drawable/bt_home"
/>
<RadioButton
android:id="@+id/btn_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:button="@null"
android:background="@drawable/bt_search"
/>
<RadioButton
android:id="@+id/btn_setting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:button="@null"
android:background="@drawable/bt_set"
/>
</RadioGroup>
</LinearLayout>
然后创建一个抽象父类,负责当按钮点击时把不同的页面加载到我们定义好的容器里。
package com.wlx.activity;
import com.example.hangzhouapp.R;
import com.example.hangzhouapp.R.id;
import com.example.hangzhouapp.R.layout;
import com.example.hangzhouapp.R.menu;
import android.app.Activity;
import android.app.ActionBar;
import android.app.ActivityGroup;
import android.app.Fragment;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.os.Build;
@SuppressWarnings("deprecation")
public abstract class BaseActivity extends ActivityGroup {
LocalActivityManager localActivityManager;
ViewGroup container;
abstract protected ViewGroup getContainer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private Intent initIntent(Class<?> cls)
{
return new Intent(this,cls).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
public void setContainer(String activityName,Class<?> typeClass)
{
if(localActivityManager == null)
{
localActivityManager = getLocalActivityManager();
}
if(container == null)
{
container = getContainer();
}
container.removeAllViews();
Activity contentActivity = localActivityManager.getActivity(activityName);
if((activityName.equals("MapNavigateDemoA5ctivity"))||(activityName.equals("HomeActivity")))
{
localActivityManager.startActivity(activityName, initIntent(typeClass));
}
else if (null == contentActivity)
{
localActivityManager.startActivity(activityName, initIntent(typeClass));
}
container.addView(
localActivityManager.getActivity(activityName).getWindow().getDecorView(),
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
}
}
接下来的工作就是在Activity中处理三个按钮的点击事件,包括更换点击之后按钮的背景,调用父类的方法改变上方的界面。代码如下:
package com.wlx.activity;
import com.example.hangzhouapp.R;
import com.example.hangzhouapp.R.layout;
import android.R.bool;
import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.content.pm.FeatureInfo;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
public class MainActivity extends BaseActivity implements OnClickListener,OnCheckedChangeListener{
private RadioButton btn_home,btn_search,btn_setting;
private LocalActivityManager localActivityManager;
private static final String HOME_ACTIVITY = "home_activity";
private static final String SEARCH_ACTIVITY = "search_activity";
private static final String SETTING_ACTIVITY = "setting_activity";
@Override
public void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
initView();
setListener();
btn_home.setChecked(true);
}
private void initView() {
// TODO Auto-generated method stub
btn_home = (RadioButton) findViewById(R.id.btn_home);
btn_search = (RadioButton)findViewById(R.id.btn_search);
btn_setting = (RadioButton)findViewById(R.id.btn_setting);
btn_home.setBackgroundResource(R.drawable.bt_home2);
btn_search.setBackgroundResource(R.drawable.bt_search);
btn_setting.setBackgroundResource(R.drawable.bt_set);
}
private void setListener() {
// TODO Auto-generated method stub
btn_home.setOnClickListener(this);
btn_search.setOnClickListener(this);
btn_setting.setOnClickListener(this);
btn_home.setOnCheckedChangeListener(this);
btn_search.setOnCheckedChangeListener(this);
btn_setting.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton compoundButton,boolean isChecked)
{
if(isChecked)
{
switch (compoundButton.getId()) {
case R.id.btn_home:
setContainer(HOME_ACTIVITY, HomeActivity.class);
break;
case R.id.btn_search:
setContainer(SEARCH_ACTIVITY, SearchActivity.class);
break;
case R.id.btn_setting:
setContainer(SETTING_ACTIVITY, SettingActivity.class);
break;
default:
break;
}
}
}
@Override
public void onClick(View view)
{
switch (view.getId()) {
case R.id.btn_home:
btn_home.setBackgroundResource(R.drawable.bt_home2);
btn_search.setBackgroundResource(R.drawable.bt_search);
btn_setting.setBackgroundResource(R.drawable.bt_set);
break;
case R.id.btn_search:
btn_home.setBackgroundResource(R.drawable.bt_home);
btn_search.setBackgroundResource(R.drawable.bt_search2);
btn_setting.setBackgroundResource(R.drawable.bt_set);
break;
case R.id.btn_setting:
btn_home.setBackgroundResource(R.drawable.bt_home);
btn_search.setBackgroundResource(R.drawable.bt_search);
btn_setting.setBackgroundResource(R.drawable.bt_set2);
break;
default:
break;
}
}
@Override
protected ViewGroup getContainer() {
// TODO Auto-generated method stub
return (ViewGroup)findViewById(R.id.container);
}
}
上方的界面是一个Activity,布局很简单,不再贴代码。