detail: v7 實作底部欄
step1: D:\project\push\Infrared\five\v7\kotlin\DemoAnalytic\app\build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
dependencies {
implementation 'com.google.firebase:firebase-core:16.0.9'
implementation 'com.android.support:design:28.0.0'
}
apply plugin: 'com.google.gms.google-services'
step2: NavigationActivity 首頁
package com.example.demoanalytic;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
public class NavigationActivity extends AppCompatActivity {
private BottomNavigationView bottomNav;
private FragmentTransaction fragmentTransaction;
private HomeFragment homeFragment;
private ResultFragment resultFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
initViews();
initFragments();
}
private void initFragments() {
homeFragment = new HomeFragment();
setFragment(homeFragment);
}
private void setFragment(Fragment fragment) {
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_home, fragment).commit();
}
private void initViews() {
bottomNav = (BottomNavigationView) findViewById(R.id.bottomNav);
bottomNav.setOnNavigationItemSelectedListener(new BottomNavi());
}
private class BottomNavi implements BottomNavigationView.OnNavigationItemSelectedListener {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.botnav_home:
homeFragment = new HomeFragment();
setFragment(homeFragment);
return true;
case R.id.botnav_result:
resultFragment = new ResultFragment();
setFragment(resultFragment);
return true;
default:
return false;
}
}
}
}
step3: 首頁布局
<?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:id="@+id/nav_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2"
android:orientation="vertical">
<!--background that changes fragments-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_gravity="center_horizontal"
android:id="@+id/frame_home">
</FrameLayout>
<!--bottom nav -->
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomNav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
app:elevation="10dp"
app:menu="@menu/botnav_menu">
</android.support.design.widget.BottomNavigationView>
</LinearLayout>
</LinearLayout>
step4: menu 底部
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/botnav_home"
android:icon="@drawable/ic_composition"
android:title="Home"/>
<item android:id="@+id/botnav_reference"
android:icon="@drawable/ic_composition"
android:title="References"/>
<item android:id="@+id/botnav_result"
android:icon="@drawable/ic_composition"
android:title="Results"/>
<item android:id="@+id/botnav_account"
android:icon="@drawable/ic_composition"
android:title="Account"/>
</menu>
step5: fragment
package com.example.demoanalytic;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class HomeFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, null);
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_purple">
</android.support.constraint.ConstraintLayout>