布局文件的实现代码如下:
LinearLayout 里面有两个布局FrameLayout 和RadioGroup 垂直排列。
FrameLayout里面放置fragment,radiogroup放置可以
切换fragment的按钮
<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="com.example.gacmy.maintab.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/fg_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_shouye"
style="@style/home_tab_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:onClick="onShouye"
android:text="test1"
android:checked="false" />
<RadioButton
android:id="@+id/rb_kaoqin"
style="@style/home_tab_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:onClick="onKaoqin"
android:text="test2"
/>
<RadioButton
android:id="@+id/rb_my"
style="@style/home_tab_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:onClick="onMy"
android:text="test3"
/>
</RadioGroup>
</LinearLayout>
</RelativeLayout>
代码实现如下:
public class MainActivity extends AppCompatActivity {
private RadioButton rb_menu1;
private RadioButton rb_menu2;
private RadioButton rb_menu3;
private Fragment fragment1;
private Fragment fragment2;
private Fragment fragment3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
rb_menu1=(RadioButton)findViewById(R.id.rb_shouye);
rb_menu2=(RadioButton)findViewById(R.id.rb_kaoqin);
rb_menu3=(RadioButton)findViewById(R.id.rb_my);
}
//切换fragment的方法。
@SuppressLint("NewApi")
private void setSelection(int index) {
resetImg();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
hideFragments(ft);
switch (index) {
case 0:
rb_menu1.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.ic_launcher, 0, 0);
rb_menu1.setTextColor(rb_menu1.getResources().getColor(R.color.colorAccent));
if (fragment1 == null) {
fragment1 = new Fragment();
ft.add(R.id.fg_content, fragment1);
} else {
ft.show(fragment1);
}
break;
case 1:
rb_menu2.setCompoundDrawablesRelativeWithIntrinsicBounds(0,R.mipmap.ic_launcher, 0, 0);
rb_menu2.setTextColor(rb_menu2.getResources().getColor(R.color.colorAccent));
if (fragment2 == null) {
fragment2 = new Fragment();
ft.add(R.id.fg_content, fragment2);
} else {
ft.show(fragment2);
}
break;
case 2:
rb_menu3.setCompoundDrawablesRelativeWithIntrinsicBounds(0,R.mipmap.ic_launcher, 0, 0);
rb_menu3.setTextColor(rb_menu3.getResources().getColor(R.color.colorAccent));
if (fragment3 == null) {
fragment3 = new Fragment();
ft.add(R.id.fg_content, fragment3);
} else {
ft.show(fragment3);
}
break;
}
ft.commit();
}
//重置切换布局的按钮
@SuppressLint("NewApi")
private void resetImg() {
rb_menu1.setCompoundDrawablesRelativeWithIntrinsicBounds(0,R.drawable.ic_photo_white_24dp, 0, 0);
rb_menu1.setTextColor(rb_menu1.getResources().getColor(R.color.colorPrimary));
rb_menu2.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.ic_photo_white_24dp, 0, 0);
rb_menu2.setTextColor(rb_menu2.getResources().getColor(R.color.colorPrimary));
rb_menu3.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.ic_photo_white_24dp, 0, 0);
rb_menu3.setTextColor(rb_menu3.getResources().getColor(R.color.colorPrimary));
}
private void hideFragments(FragmentTransaction ft) {
if (fragment1 != null) {
ft.hide(fragment1);
}
if (fragment2 != null) {
ft.hide(fragment2);
}
if (fragment3 != null) {
ft.hide(fragment3);
}
}
public void onShouye(View view) {
setSelection(0);
}
public void onKaoqin(View view) {
setSelection(1);
}
public void onMy(View view) {
setSelection(2);
}
}