效果:
项目结构:
需要用到design依赖: 现在是:implementation ‘com.android.support:design:28.0.0’
代码:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:id="@+id/frame_fragment"
android:layout_width="fill_parent"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/bottomNavigationView"/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="0dp"
android:layout_height="0dp"
app:itemBackground="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_percent="0.09"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:itemIconTint="@drawable/home_bottom_tab_color_selsctor"
app:itemTextColor="@drawable/home_bottom_tab_color_selsctor"
app:menu="@menu/bottom_navigation_main" />
</android.support.constraint.ConstraintLayout>
my_fragment.xml(其他两个类似不在累述)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="我的界面"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
</android.support.constraint.ConstraintLayout>
home_bottom_tab_color_selsctor.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/black" android:state_checked="false" />
<item android:color="@color/home_bottom_tab_blue" android:state_checked="true" />
</selector>
home_bottom_tab_my_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="false"
android:drawable="@drawable/home_bottom_tab_icon_my_normal"
/>
<item
android:state_checked="true"
android:drawable="@drawable/home_bottom_tab_icon_my_highlight"
/>
</selector>
MainActivity
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.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private BottomNavigationView bottomNavigationView;
private Fragment work_fragment;
private Fragment message_fragment;
private Fragment my_fragment;
private Fragment[] fragments;
private int lastfragment;//用于记录上个选择的Fragment
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFragment();
}
private void initFragment() {
work_fragment = new Work_Fragment();
message_fragment = new Message_Fragment();
my_fragment = new My_Fragment();
fragments = new Fragment[]{message_fragment, work_fragment, my_fragment};
lastfragment = 0;
getSupportFragmentManager().beginTransaction().replace(R.id.frame_fragment, message_fragment).show(message_fragment).commit();
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationView);
bottomNavigationView.setOnNavigationItemSelectedListener(changeFragment);
}
private BottomNavigationView.OnNavigationItemSelectedListener changeFragment = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_message: {
if (lastfragment != 0) {
switchFragment(lastfragment, 0);
lastfragment = 0;
}
return true;
}
case R.id.action_work: {
if (lastfragment != 1) {
switchFragment(lastfragment, 1);
lastfragment = 1;
}
return true;
}
case R.id.action_my: {
if (lastfragment != 2) {
switchFragment(lastfragment, 2);
lastfragment = 2;
}
return true;
}
}
return false;
}
};
//切换Fragment
private void switchFragment(int lastfragment, int index) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.hide(fragments[lastfragment]);//隐藏上个Fragment
if (fragments[index].isAdded() == false) {
transaction.add(R.id.frame_fragment, fragments[index]);
}
transaction.show(fragments[index]).commitAllowingStateLoss();
}
/* public boolean onKeyUp(int keyCode, KeyEvent event) {
//在主界面,连击连两下退出程序
if(keyCode==KeyEvent.KEYCODE_BACK){
long secondTime=System.currentTimeMillis();
if(secondTime-firstTime>800){
Toast.makeText(Home_Activity.this,"再按一次返回键退出",Toast.LENGTH_SHORT).show();
firstTime=secondTime;
return true;
}else{
WelcomeActivity.activity.finish();
System.exit(0);
}
}
return super.onKeyUp(keyCode, event);
}*/
}
My_fragment(其他两个类似不在累述)
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class My_Fragment extends Fragment {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.my_fragment,null);
return view;
}
}
版权声明:本文为qq_21480607原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。