2 回答
TA贡献1862条经验 获得超7个赞
菜单项视图
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_home"
android:icon="@drawable/ic_home_blue_48dp"
android:title="Home" />
<item
android:id="@+id/action_menu"
android:icon="@drawable/ic_apps_black_24dp"
android:title="Menu"
/>
<item
android:id="@+id/action_msg"
android:icon="@drawable/ic_chat_black_24dp"
android:title="Message Inbox"
/>
</menu>
单击侦听器
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item)
{
Fragment selectedFragment = null;
switch (item.getItemId())
{
case R.id.action_home:
/*hide extra toolbars*/
hideViews();
/*hide back button*/
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
selectedFragment = new TimelineFragment();
break;
case R.id.action_menu:
hideViews();
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
selectedFragment = new HomeFragment();
break;
case R.id.action_msg:
hideViews();
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
selectedFragment = new ChatFragment();
break;
}
TA贡献2039条经验 获得超7个赞
使用在菜单项中设置的自定义布局,例如:
<item
android:id="@+id/menu_item_1"
app:showAsAction="always"
app:actionLayout="@layout/custom_menu_item_1" />
创建一个布局文件并将此文件设置为 app:actionLayout。
custom_menu_item_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/menu_drawable"
android:orientation="vertical">
<ImageView ... marginBottom=".." />
<TextView ... />
</LinearLayout>
添加回答
举报