样式xml文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity"> <FrameLayout android:id="@+id/home_xianshi" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9"/> <RadioGroup android:id="@+id/home_bottom_gadio" android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" android:layout_weight="1"> <RadioButton android:id="@+id/home_bottom_btn1" style="@style/bottom_bar" android:background="@color/beijing" android:text="首页" android:textColor="@color/white" android:layout_width="match_parent" /> <RadioButton android:id="@+id/home_bottom_btn2" style="@style/bottom_bar" android:background="@color/beijing" android:text="全部服务" android:textColor="@color/white" android:layout_width="match_parent" /> <RadioButton android:id="@+id/home_bottom_btn3" style="@style/bottom_bar" android:background="@color/beijing" android:text="智慧党建" android:textColor="@color/white" android:layout_width="match_parent" /> <RadioButton android:id="@+id/home_bottom_btn4" style="@style/bottom_bar" android:background="@color/beijing" android:text="新闻" android:textColor="@color/white" android:layout_width="match_parent" /> <RadioButton android:id="@+id/home_bottom_btn5" style="@style/bottom_bar" android:background="@color/beijing" android:text="个人中心" android:textColor="@color/white" android:layout_width="match_parent" /> </RadioGroup> </LinearLayout>
底部导航栏设置自定义样式
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="bottom_bar"> <item name="android:layout_width">0dp</item> <item name="android:layout_height">match_parent</item> <item name="android:layout_weight">1</item> <item name="android:button">@null</item> <item name="background">@drawable/home_bottom_bar</item> <item name="android:textColor">@drawable/home_bottom_txt</item> <item name="android:textSize">15sp</item> <item name="android:gravity">center</item> </style> </resources>
在MainActivity
public class MainActivity extends AppCompatActivity { private FrameLayout mHomeXianshi; private RadioGroup mHomeBottomGadio; private RadioButton mHomeBottomBtn1; private RadioButton mHomeBottomBtn2; private RadioButton mHomeBottomBtn3; private RadioButton mHomeBottomBtn4; private RadioButton mHomeBottomBtn5; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initview(); initData(); } private void initData() { mHomeBottomGadio.check(R.id.home_bottom_btn1); setFragment(new FristFragment()); mHomeBottomGadio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.home_bottom_btn1: setFragment(new FristFragment()); break; case R.id.home_bottom_btn2: setFragment(new AllserviceFragment()); break; case R.id.home_bottom_btn3: setFragment(new DangJianFragment()); break; case R.id.home_bottom_btn4: setFragment(new NewsFragment()); break; case R.id.home_bottom_btn5: setFragment(new UserFragment()); break; } } }); } private void setFragment(Fragment fragment) { FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.home_xianshi,fragment).commit(); } private void initview() { mHomeXianshi = findViewById(R.id.home_xianshi); mHomeBottomGadio = findViewById(R.id.home_bottom_gadio); mHomeBottomBtn1 = findViewById(R.id.home_bottom_btn1); mHomeBottomBtn2 = findViewById(R.id.home_bottom_btn2); mHomeBottomBtn3 = findViewById(R.id.home_bottom_btn3); mHomeBottomBtn4 = findViewById(R.id.home_bottom_btn4); mHomeBottomBtn5 = findViewById(R.id.home_bottom_btn5); } }