个人网站:极客时代
用RadioGroup设置功能menu,通过点击不同的RadioButton切换不同的Fragment。
布局文件:activity_main.xml(除此之外,就是几个Fragment的布局文件了)
<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.Ray.testshow.MainActivity"
android:orientation="vertical">"
<!-- 装fragment的容器 -->
<FrameLayout android:id="@+id/fragment_container"
android:layout_height="0px"
android:layout_width="match_parent"
android:layout_weight="7">
</FrameLayout>
<RadioGroup android:id="@+id/RGMenu"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:layout_gravity="bottom"
android:gravity="center"
android:orientation="horizontal">"
<!-- 将RadioGroup放在最底部,里面的按钮居中显示 -->
<RadioButton style="@style/rb_style"
android:drawableTop="@drawable/menu1_click"
android:text="Menu1"/>
<RadioButton style="@style/rb_style"
android:drawableTop="@drawable/menu2_click"
android:text="Menu2"/>
<RadioButton style="@style/rb_style"
android:drawableTop="@drawable/menu3_click"
android:text="Menu3"/>
<RadioButton style="@style/rb_style"
android:drawableTop="@drawable/menu4_click"
android:text="Menu4"/>
<RadioButton style="@style/rb_style"
android:drawableTop="@drawable/menu5_click"
android:text="Menu5"/>
</RadioGroup>
</LinearLayout>
menu1_click.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 设置RadioButton在未点击、点击及选中的时候的图片显示,以与其他的RadiosButton区分 -->
<item android:drawable="@drawable/menu1" android:state_pressed="true"/>
<item android:drawable="@drawable/menu1" android:state_checked="true"/>
<item android:drawable="@drawable/menu1_be"/>
<!-- 把默认值放在最后,否则会一直保持在默认状态无法改变 -->
</selector>
Java文件:
MainActivity.java
package com.Ray.testshow;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioGroup;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//默认的fragment
FragmentManager fManager = getFragmentManager();
FragmentTransaction fTransaction = fManager.beginTransaction();
Fragment fragment = FragmentCheck.getFragmentById(1);
fTransaction.add(R.id.fragment_container, fragment);
fTransaction.commit();
//通过点击不同的RadioButton进行切换
RadioGroup rGroup = (RadioGroup)findViewById(R.id.RGMenu);
rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
FragmentManager fManager = getFragmentManager();
FragmentTransaction fTransaction = fManager.beginTransaction();
Fragment fragment = FragmentCheck.getFragmentById(checkedId);
fTransaction.replace(R.id.fragment_container, fragment);
fTransaction.commit();
}
});
}
}
FragmentCheck.java(通过传入不同的值,来生成不同的fragment对象)
package com.Ray.testshow;
import android.app.Fragment;
public class FragmentCheck {
public static Fragment getFragmentById(int checkedID) {
Fragment fragment = null;
switch (checkedID) {
case 1:
fragment=new FragmentMenu1();
break;
case 2:
fragment=new FragmentMenu2();
break;
case 3:
fragment=new FragmentMenu3();
break;
case 4:
fragment=new FragmentMenu4();
break;
case 5:
fragment=new FragmentMenu5();
break;
default:
break;
}
return fragment;
}
}
其他的java文件都是fragment对应的了。
写了一个比较简单的Demo,虽然不是很好看,但是能看出怎样用的。换几张RadioButton的图片,效果会好很多,没时间去自己P了,丑些。
示例代码下载地址:
http://download.csdn.net/detail/darkxionghaizi/9078709