一般App都是底部4、5个导航栏,然后,上面是一个FrameLayout用于存放不同tab对应的页面
基本上每次的代码都差不多,每次都要写肯定要烦,所以总结一下,以后直接拿来用就ok了
xml文件:
<?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">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="56dp"
android:id="@+id/toolbar"/>
<FrameLayout
android:id="@+id/fl_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/darkgray"/>
<RadioGroup
android:id="@+id/rg_main"
android:layout_width="match_parent"
android:layout_height="56dp"
android:paddingTop="2dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_home"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/transparent"
android:button="@null"
android:drawableTop="@drawable/selector_home"
android:gravity="center"
android:text="@string/tab_shouye"
/>
<RadioButton
android:id="@+id/rb_classify"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/transparent"
android:button="@null"
android:drawableTop="@drawable/selector_lanmu"
android:gravity="center"
android:text="@string/tab_lanmu"/>
<RadioButton
android:id="@+id/rb_shop"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/transparent"
android:button="@null"
android:drawableTop="@drawable/selector_zhibo"
android:gravity="center"
android:text="@string/tab_zhibo"/>
<RadioButton
android:id="@+id/rb_mine"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/transparent"
android:button="@null"
android:drawableTop="@drawable/selector_wode"
android:gravity="center"
android:text="@string/tab_wode"/>
</RadioGroup>
</LinearLayout>
需要注意的是,去除radiobutton的前面的圆圈,是用android:button="@null"这个玩意儿
另外,每次点击底部导航的时候图片的颜色和文字的颜色会跟着一起改变
这个我们写个选择器图像资源就可以了
类似下面这样
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/btn_tabbar_home_normal" android:state_checked="false"/>
<item android:drawable="@mipmap/btn_tabbar_home_selected" android:state_checked="true"/>
</selector>
然后,是主Activity的代码:
package com.johnwharton.quanmintv;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.Window;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.johnwharton.quanmintv.ui.home.HomeFragment;
import com.johnwharton.quanmintv.ui.lanmu.LanmuFragment;
import com.johnwharton.quanmintv.ui.wode.WodeFragment;
import com.johnwharton.quanmintv.ui.zhibo.ZhiboFragment;
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{
private HomeFragment mHomeFragment;
private LanmuFragment mLanmuFragment;
private ZhiboFragment mZhiboFragment;
private WodeFragment mWodeFragment;
private RadioGroup rg_main;
private Toolbar mToolbar;
public void initView() {
mToolbar = (Toolbar) findViewById(R.id.toolbar);
rg_main = (RadioGroup) findViewById(R.id.rg_main);
rg_main.setOnCheckedChangeListener(this);
selectedFragment(0);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initData() {
mToolbar.setNavigationIcon(R.mipmap.img_rec_logo);
mToolbar.inflateMenu(R.menu.base_toolbar_menu);
mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.action_gift:
Toast.makeText(MainActivity.this,"gift",Toast.LENGTH_SHORT).show();
break;
case R.id.action_heart:
Toast.makeText(MainActivity.this,"heart",Toast.LENGTH_SHORT).show();
break;
case R.id.action_search:
Toast.makeText(MainActivity.this,"search",Toast.LENGTH_SHORT).show();
break;
}
return true;
}
});
}
protected void selectedFragment(int index) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
hideFragment(transaction);
switch (index){
case 0:
if (mHomeFragment == null){
mHomeFragment = HomeFragment.getInstance();
transaction.add(R.id.fl_container,mHomeFragment);
}else {
transaction.show(mHomeFragment);
}
break;
case 1:
if (mLanmuFragment == null){
mLanmuFragment = LanmuFragment.getInstance();
transaction.add(R.id.fl_container,mLanmuFragment);
}else {
transaction.show(mLanmuFragment);
}
break;
case 2:
if (mZhiboFragment == null){
mZhiboFragment = ZhiboFragment.getInstance();
transaction.add(R.id.fl_container,mZhiboFragment);
}else {
transaction.show(mZhiboFragment);
}
break;
case 3:
if (mWodeFragment == null){
mWodeFragment = WodeFragment.getInstance();
transaction.add(R.id.fl_container,mWodeFragment);
}else {
transaction.show(mWodeFragment);
}
break;
default:
break;
}
transaction.commit();
}
protected void hideFragment(FragmentTransaction transaction) {
if (mHomeFragment != null){
transaction.hide(mHomeFragment);
}
if (mLanmuFragment != null){
transaction.hide(mLanmuFragment);
}
if (mZhiboFragment != null){
transaction.hide(mZhiboFragment);
}
if (mWodeFragment != null){
transaction.hide(mWodeFragment);
}
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.rb_home:
selectedFragment(0);
break;
case R.id.rb_classify:
selectedFragment(1);
break;
case R.id.rb_shop:
selectedFragment(3);
break;
case R.id.rb_mine:
selectedFragment(4);
break;
}
}
}
Fragment我是这样写的,然后你需要什么,就自定义自己的Fragment吧
public class LanmuFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_lanmu,container,false);
return view;
}
public static LanmuFragment getInstance(){
return new LanmuFragment();
}
}
网友评论