RadioButton+FrameLayout+Fragment实现底部导航

截图:

布局:

<?xml version="1.0" encoding="utf-8"?>
<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">

    <include
        android:id="@+id/ic"
        layout="@layout/include_title"
        android:layout_width="match_parent"
        android:layout_height="48dp" />

    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"
        android:background="@color/dhlqianhui"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb_home"
            style="@style/Custom.TabRadioButton"
            android:drawableTop="@drawable/tab_sign_selector" />

        <RadioButton
            android:id="@+id/rb_mall"
            style="@style/Custom.TabRadioButton"
            android:drawableTop="@drawable/tab_record_selector" />

        <View style="@style/Custom.TabRadioButton" />

        <RadioButton
            android:id="@+id/rb_video"
            style="@style/Custom.TabRadioButton"
            android:drawableTop="@drawable/tab_contact_selector" />

        <RadioButton
            android:id="@+id/rb_my"
            style="@style/Custom.TabRadioButton"
            android:drawableTop="@drawable/tab_setting_selector" />
    </RadioGroup>

    <LinearLayout
        android:id="@+id/ll_sign"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:orientation="vertical"
        android:padding="5dp">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:src="@drawable/ic_publish" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="发布" />
    </LinearLayout>

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/rg"
        android:layout_below="@id/ic" />
</RelativeLayout>

样式:

<style name="Custom.TabRadioButton">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_weight">1</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:padding">5dp</item>
        <item name="android:gravity">center</item>
        <item name="android:button">@null</item>
        <item name="android:textSize">0sp</item>
        <item name="android:textColor">@color/tab_text_color_selector</item>
    </style>

主要java代码:

package com.shanjing.hotattention.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.base.commonlib.BaseActivity;
import com.base.commonlib.utils.StatusBarUtil;
import com.base.commonlib.zxing.android.CaptureActivity;
import com.shanjing.hotattention.R;
import com.shanjing.hotattention.fragment.HotHomeFragment;
import com.shanjing.hotattention.fragment.HotMallFragment;
import com.shanjing.hotattention.fragment.HotMyFragment;
import com.shanjing.hotattention.fragment.HotVideoFragment;

import java.io.ByteArrayOutputStream;

@Route(path = "/hotattention/main")
public class HotAttentionHomeActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {

    public RadioGroup rg;
    public LinearLayout ll_sign;
    private RadioButton rb_home, rb_mall, rb_video, rb_my;
    private HotHomeFragment hotHomeFragment;
    private HotMallFragment hotMallFragment;
    private HotVideoFragment hotVideoFragment;
    private HotMyFragment hotMyFragment;

    private void assignViews() {
        iv_scan = findViewById(R.id.iv_scan);
        rg = findViewById(R.id.rg);
        ll_sign = findViewById(R.id.ll_sign);
        rb_home = findViewById(R.id.rb_home);
        rb_mall = findViewById(R.id.rb_mall);
        rb_video = findViewById(R.id.rb_video);
        rb_my = findViewById(R.id.rb_my);
        rg.setOnCheckedChangeListener(this);
        ll_sign.setOnClickListener(this);
        rb_home.setChecked(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hot_attention_home);
        //用来设置整体下移,状态栏沉浸
        StatusBarUtil.getStatusBarHeight(HotAttentionHomeActivity.this);
        StatusBarUtil.setRootViewFitsSystemWindows(this, false);
        StatusBarUtil.setStatusBarColor(HotAttentionHomeActivity.this, Color.parseColor("#ffffff"));//设置背景颜色
        assignViews();
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideAllFragment(transaction);
        switch (checkedId) {
            case R.id.rb_home:
                if (hotHomeFragment == null) {
                    hotHomeFragment = new HotHomeFragment();
                    transaction.add(R.id.fragment_container, hotHomeFragment);
                } else {
                    transaction.show(hotHomeFragment);
                }
                break;
            case R.id.rb_mall:
                if (hotMallFragment == null) {
                    hotMallFragment = new HotMallFragment();
                    transaction.add(R.id.fragment_container, hotMallFragment);
                } else {
                    transaction.show(hotMallFragment);
                }
                break;
            case R.id.rb_video:
                if (hotVideoFragment == null) {
                    hotVideoFragment = new HotVideoFragment();
                    transaction.add(R.id.fragment_container, hotVideoFragment);
                } else {
                    transaction.show(hotVideoFragment);
                }
                break;
            case R.id.rb_my:
                if (hotMyFragment == null) {
                    hotMyFragment = new HotMyFragment();
                    transaction.add(R.id.fragment_container, hotMyFragment);
                } else {
                    transaction.show(hotMyFragment);
                }
                break;
        }
        transaction.commit();
    }

    public void hideAllFragment(FragmentTransaction transaction) {
        if (hotHomeFragment != null) {
            transaction.hide(hotHomeFragment);
        }
        if (hotMallFragment != null) {
            transaction.hide(hotMallFragment);
        }
        if (hotVideoFragment != null) {
            transaction.hide(hotVideoFragment);
        }
        if (hotMyFragment != null) {
            transaction.hide(hotMyFragment);
        }
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.ll_sign) {//跳转到发布页
            startActivity(new Intent(this, IssueActivity.class));
        }
    }
}

再新建几个fragment作为各个模块就可以了

猜你喜欢

转载自blog.csdn.net/juer2017/article/details/90747627
今日推荐