<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.zhangt.fragmenttwo.MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <RadioGroup android:id="@+id/rg" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentBottom="true"> <RadioButton android:id="@+id/rb_1" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="首页" android:button="@null" android:checked="true" /> <RadioButton android:id="@+id/rb_2" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="分类" android:button="@null" /> <RadioButton android:id="@+id/rb_3" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="发现" android:button="@null" /> <RadioButton android:id="@+id/rb_4" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="购物车" android:button="@null" /> <RadioButton android:id="@+id/rb_5" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="我的" android:button="@null" /> </RadioGroup> <FrameLayout android:id="@+id/frag" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/rg"></FrameLayout> </RelativeLayout> </RelativeLayout>
//
添加 Fragment
package com.example.zhangt.fragmenttwo; import android.support.annotation.IdRes; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.RadioGroup; import com.example.zhangt.fragmenttwo.Fragement.Five; import com.example.zhangt.fragmenttwo.Fragement.Four; import com.example.zhangt.fragmenttwo.Fragement.One; import com.example.zhangt.fragmenttwo.Fragement.Three; import com.example.zhangt.fragmenttwo.Fragement.Two; public class MainActivity extends AppCompatActivity { private RadioGroup ra; @Override // Framnent 里套Fragment Viewpager+Fragment 联动 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ra = (RadioGroup) findViewById(R.id.rg); ra.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { switch (checkedId){ case R.id.rb_1: addfragment(new One()); break; case R.id.rb_2: addfragment(new Two()); break; case R.id.rb_3: addfragment(new Three()); break; case R.id.rb_4: addfragment(new Four()); break; case R.id.rb_5: addfragment(new Five()); break; default: break; } } }); } public void addfragment(Fragment f){ FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.replace(R.id.frag,f).commit(); } }
///
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:color="@color/green"></item> <item android:state_checked="true" android:color="@color/green"></item> <item android:state_pressed="true" android:color="@color/green"></item> <item android:color="@color/black"/> </selector>
<RadioButton android:clickable="true" android:id="@+id/rb_5" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /// android:textColor="@drawable/text_selector" android:text="我的" android:button="@null" />