android 用Radio实现的底部导航
package test.com.bottomnavigationradio; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.support.v4.app.Fragment; import android.widget.RadioGroup; public class MainActivity extends AppCompatActivity { RadioGroup radioGroup; OneFragment oneFragment; TwoFragment twoFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); radioGroup=(RadioGroup)findViewById(R.id.radioGroup); oneFragment=new OneFragment(); twoFragment=new TwoFragment(); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { Fragment fragment=null; switch (checkedId) { case R.id.one: fragment=new OneFragment();//第一页 break; case R.id.two: fragment=new TwoFragment();//第二页 break; } JumpFragment(fragment); } }); radioGroup.check(R.id.one);//默认选择 } /** * 跳转 * @param fragment */ private void JumpFragment(Fragment fragment) { FragmentManager fragmentManager=getSupportFragmentManager(); fragmentManager.beginTransaction().replace(R.id.main_fragment,fragment).commit(); } @Override public boolean onCreateOptionsMenu(Menu menu) { return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { return super.onOptionsItemSelected(item); }
}
-------------------------
<?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"> <FrameLayout android:id="@+id/main_fragment" android:layout_width="match_parent" android:layout_height="473dp"> </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="2dp" android:background="#454343" ></LinearLayout> <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="75dp" android:orientation="horizontal" android:gravity="center"> <RadioButton android:id="@+id/one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="首页" android:button="@null" android:drawableTop="@drawable/bottom_one" android:textColor="@drawable/selector_color_home"/> <RadioButton android:id="@+id/two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第二页" android:button="@null" android:layout_marginLeft="10dp" android:drawableTop="@drawable/bottom_one" android:textColor="@drawable/selector_color_home"/> </RadioGroup> </LinearLayout>-----------------------------
package test.com.bottomnavigationradio; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; //第一页 public class OneFragment extends Fragment { @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_one, container, false); } }
-----------------
package test.com.bottomnavigationradio; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; //第二页 public class TwoFragment extends Fragment { public TwoFragment() { super(); } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup,Bundle savedInstanceState) { return layoutInflater.inflate(R.layout.fragment_two,viewGroup,false); } }
--------------
<?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"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="首页"/> </LinearLayout>
-------------
<?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"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="第二页"/> </LinearLayout>