android界面底部BottomBar以及fragment

推荐一个介绍fragment的好博客
BottomBar开源地址
一个非常完善的Fragment的开源地址,很少在github上看到这么完善的来自天朝的代码。

使用Fragment的原因是要一个漂亮的底栏,可以根据点击的按钮不同加载不同的fragment,这个应该就是但activity对多fragment。

以下是具体代码:

1、 bottomBar.xml


<tabs>
    <tab
        id="@+id/tab_day"
        icon="@drawable/newspaper48"
        title="日报" />
    <tab
        id="@+id/tab_exe"
        icon="@drawable/text48"

        title="报表" />
    <tab
        id="@+id/tab_pic"
        icon="@drawable/chart48"
        title="图标" />
    <tab
        id="@+id/tab_user"
        icon="@drawable/user48"
        title="用户" />
tabs>

2、 装bottomBar的relativeLayout的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <RelativeLayout
        android:id="@+id/contentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottomBar" />
    <com.roughike.bottombar.BottomBar
        android:id="@+id/bottomBar"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        app:bb_tabXmlResource="@xml/bottombar_tabs" />
RelativeLayout>

3、activity_main.xml的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
    
    <include layout="@layout/toolbar_layout"/>
    <include layout="@layout/toplayout"/>
LinearLayout>

4、main_activity的代码

package com.hb.bottomlast;

import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Layout;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.github.jjobes.slidedatetimepicker.SlideDateTimeListener;
import com.github.jjobes.slidedatetimepicker.SlideDateTimePicker;
import com.hb.bottomlast.Persion1_fragment;
import com.hb.bottomlast.Persion2_fragment;
import com.hb.bottomlast.Persion3_fragment;
import com.hb.bottomlast.R;
import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnTabReselectListener;
import com.roughike.bottombar.OnTabSelectListener;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity{
    double nLenStart = 0; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
        }
        final View layout = findViewById(R.id.contentContainer);

        BottomBar bottomBar = (BottomBar) findViewById(R.id.bottomBar);
        bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(@IdRes int tabId) {
                Object ob=null;
                switch (tabId) {
                    case R.id.tab_day:
                        ob  = new Persion1_fragment();
                        break;
                    case R.id.tab_exe:
                        ob  = new Persion2_fragment();
                        break;
                    case R.id.tab_pic:
                        ob  = new Persion3_fragment();
                        break;
                    case R.id.tab_user:
                        ob  = new Persion4_fragment();
                        break;
                }  getSupportFragmentManager().beginTransaction().replace(R.id.contentContainer,(Fragment) ob).commit();
            }
        });
        bottomBar.setOnTabReselectListener(new OnTabReselectListener() {
            @Override
            public void onTabReSelected(@IdRes int tabId) {    getSupportFragmentManager().beginTransaction().replace(R.id.contentContainer,new Persion1_fragment()).commit();
            }
        });
    }
}

剩下的别忘了导入

dependencies {
compile ‘com.roughike:bottom-bar:2.0.2’
}

最后还要建立四个fragment一一对应。

你可能感兴趣的:(GitHub,android,bottomBar)