类所在包package android.support.design.widget;
效果图
java代码
public static void generateFootMenu(ViewGroup rootView, int defaultSelected){
Activity activity = getActivityFromView(rootView);
if(activity == null){
Toast.makeText(rootView.getContext(), "Render Foot Menu Eror", Toast.LENGTH_SHORT).show();
return;
}
BottomNavigationView foot = new BottomNavigationView(rootView.getContext());
foot.inflateMenu(R.menu.footmenu);
foot.setItemIconTintList(rootView.getContext().getResources().getColorStateList(R.color.bnav_setting_btn_text_selector));
foot.setItemTextColor(rootView.getContext().getResources().getColorStateList(R.color.bnav_setting_btn_text_selector));
//设置背景色 此处是设置的背景样式xml 自定义
foot.setBackgroundResource(R.drawable.shadow);
rootView.addView(foot);
foot.setSelectedItemId(defaultSelected);
foot.setOnNavigationItemSelectedListener((item) -> {
switch (item.getItemId()) {
case R.id.footer_home:
if (activity instanceof MainActivity){
return false;
}
Intent homeintent = new Intent(rootView.getContext(), MainActivity.class);
activity.startActivityForResult(homeintent, 1);
return true;
case R.id.footer_my_info:
if (activity instanceof MyInfoActivity){
return false;
}
Intent intent1 = new Intent(rootView.getContext(), MyInfoActivity.class);
activity.startActivityForResult(intent1, 1);
return true;
}
return false;
});
}
public static Activity getActivityFromView(View view) {
if (null != view) {
Context context = view.getContext();
while (context instanceof ContextWrapper) {
if (context instanceof Activity) {
return (Activity) context;
}
context = ((ContextWrapper) context).getBaseContext();
}
}
return null;
}
底部菜单样式文件 footmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:title="主页"
android:id="@+id/footer_home"
android:icon="@mipmap/home"
android:textColor="#2c2c2c"
android:background="@drawable/btn_white"
android:textSize="@dimen/textsize_l"
/>
<item
android:id="@+id/footer_my_info"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/btn_white"
android:icon="@mipmap/my"
android:textColor="#2c2c2c"
android:title="@string/title_my_info"
android:textSize="@dimen/textsize_l"
/>
</menu>
使用
界面xml
<LinearLayout 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"
android:gravity="center_horizontal"
android:id="@+id/context"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical" >
<Button
android:id="@+id/sign_in_button"
style="@style/BtnPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/action_sign_in"
android:textStyle="bold" />
<TextView
android:id="@+id/server_set_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="通用设置" />
</LinearLayout>
<!-- 占位布局 此处新增一个布局(可以是界面最后一行的布局)
android:layout_height="0dp"
android:layout_weight="1"
-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
java文件
@BindView(R.id.context)
LinearLayout context;
@SuppressLint("CommitPrefEdits")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
UiUtil.generateFootMenu(context, R.id.footer_home);
}