感谢您的光临。所以我的问题是,菜单源文件中bottom_navigation_menu.xml中的图标不可见。我尝试了Google,Stackoverflow,这是我尝试过的东西,但仍然不可见:重新启动Android Studio,添加选择器,将依赖项更改为androidX,将类更改为androidX,重新阅读文档。
希望有人可以帮助我...谢谢!!!
我正在使用Android Studio 3.6.3,Gradle 6.4
类文件
package com.example.todayilearnedbeta;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class AddEntriesActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addentries);
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavView_Bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(2);
menuItem.setChecked(true);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.ic_main:
Intent intent0 = new Intent(AddEntriesActivity.this, MainActivity.class);
startActivity(intent0);
break;
case R.id.id_search:
Intent intent1 = new Intent(AddEntriesActivity.this, SearchActivity.class);
startActivity(intent1);
break;
case R.id.ic_add_entries:
break;
case R.id.ic_past_entries:
Intent intent3 = new Intent(AddEntriesActivity.this, PastEntriesActivity.class);
startActivity(intent3);
break;
case R.id.ic_profile:
Intent intent4 = new Intent(AddEntriesActivity.this, ProfileActivity.class);
startActivity(intent4);
break;
}
return false;
}
});
}
}
布局XML代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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="match_parent"
android:layout_height="match_parent"
tools:context=".AddEntriesActivity"
android:background="@drawable/add_entry_activity_background">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:id="@+id/bottomBar"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/bottomNavView_Bar"
app:menu="@menu/bottom_navigation_menu"
app:itemIconTint="@drawable/nav_item_color_state"
app:itemBackground="@color/colorPrimary"
app:itemTextColor="@drawable/nav_item_color_state">
</com.google.android.material.bottomnavigation.BottomNavigationView>
</RelativeLayout>
bottom_navigation_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/ic_main"
android:icon="@drawable/ic_main"
android:title="MainLogo"
/>
<item
android:id="@+id/ic_search"
android:icon="@drawable/ic_search"
android:enabled="true"
android:title="Search"
/>
<item
android:id="@+id/ic_add_entries"
android:icon="@drawable/ic_add_entries"
android:enabled="true"
android:title="AddEntries"
/>
<item
android:id="@+id/ic_past_entries"
android:icon="@drawable/ic_past_entries"
android:enabled="true"
android:title="PastEntries"
/>
<item
android:id="@+id/ic_profile"
android:icon="@drawable/ic_profile"
android:enabled="true"
android:title="Backup"
/>
导航栏项目颜色选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="ffffff" android:state_checked="true" />
<item android:color="#000000" android:state_checked="false" />
</selector>
依赖关系(此处仅显示我实现了最新的实质性依赖关系。
dependencies {
implementation 'com.google.android.material:material:1.2.0-alpha06'
}
答案 0 :(得分:0)
在您的buttom_navigatiom_menu.xml中,添加此项以设置可见性状态
android:visible="true"
确保您向每个图标项添加可见性状态编码
示例
<item
android:id="@+id/ic_add_entries"
android:icon="@drawable/ic_add_entries"
android:visible="true"
android:enabled="true"
android:title="AddEntries"
/>
如果这不适用于您,请尝试以下操作: