我已经实现BottomNavigationView了新支持库25.0.0中提供的功能.这是我的代码
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@drawable/text"
app:itemTextColor="@drawable/text"
app:menu="@menu/bottom_navigation_main" />
Run Code Online (Sandbox Code Playgroud)
并且text.xml可绘制
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@android:color/white" android:state_enabled="true" />
<item android:color="@color/colorPrimaryDark" android:state_enabled="false" />
</selector>
Run Code Online (Sandbox Code Playgroud)
使用此代码,我可以在单击菜单项时更改文本颜色,但是当我对其应用相同的app:itemBackground内容时显示错误<item> tag requires a 'drawable' attribute or child tag defining a drawable.
这就是我的尝试 app:itemBackground
app:itemBackground="@drawable/text"
Run Code Online (Sandbox Code Playgroud)
所以我的问题是如何更改所选菜单项的背景颜色?
我想根据某些条件动态隐藏BottomNavigationView的菜单项.我试过以下但是没有用.
mBottomNavigationView.getMenu()
.findItem(R.id.item_name)
.setVisible(false);
mBottomNavigationView.invalidate();
Run Code Online (Sandbox Code Playgroud) 我创建了一些应用程序,我想在其中插入一个 BottomNavigationView。
该代码运行良好,但是一旦我将 gradle 更改为 androidx,它就停止工作了。
我的布局中的组件 (activity_about):
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_navigation"
app:itemBackground="@color/colorWhite"
app:itemIconTint="@drawable/bottom_navigation_foreground"
app:itemTextColor="@drawable/bottom_navigation_foreground" />
Run Code Online (Sandbox Code Playgroud)
菜单文件是:
<?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/navigation_library"
android:enabled="true"
app:showAsAction="ifRoom"
android:title="Library"
android:icon="@drawable/ic_home_black_24dp"/>
<item
android:id="@+id/navigation_search"
android:enabled="true"
app:showAsAction="ifRoom"
android:title="Search"
android:icon="@drawable/ic_search_black_24dp"/>
<item
android:id="@+id/navigation_profile"
android:enabled="true"
app:showAsAction="ifRoom"
android:title="Profile"
android:icon="@drawable/ic_account_circle_black_24dp"/>
</menu>
Run Code Online (Sandbox Code Playgroud)
可绘制文件(bottom_navigation_foreground):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@color/colorPurpleFont" />
<item android:state_checked="false" android:color="@color/Gray" />
</selector>
Run Code Online (Sandbox Code Playgroud)
我的代码如下:
public class AboutActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { …Run Code Online (Sandbox Code Playgroud) android bottomnavigationview material-components material-components-android androidx
我正在使用底部导航栏,但我没有得到完美的底部导航栏.
我的MainActivity班级:
public class MainActivity extends AppCompatActivity {
private static final String SELECTED_ITEM = "selected_item";
private BottomNavigationView bottomNavigationView;
private Toolbar toolbar;
private MenuItem menuItemSelected;
private int mMenuItemSelected;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
selectFragment(item);
return true;
}
});
//Always load first fragment as default
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frameLayout, new AnnouncementFragment());
fragmentTransaction.commit();
if (savedInstanceState != null) { …Run Code Online (Sandbox Code Playgroud) android android-layout android-fragments bottomnavigationview
我正在BottomNavigationViewAndroid应用中实现导航.我正在使用Fragments为每个选项卡设置内容.
我知道如何为每个选项卡设置一个片段,然后在单击选项卡时切换片段.但是,如何为每个选项卡分别设置一个后备堆栈?以下是设置一个片段的代码:
Fragment selectedFragment = ItemsFragment.newInstance();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content, selectedFragment);
transaction.commit();
Run Code Online (Sandbox Code Playgroud)
例如,FragmentA和B将位于选项卡1下,FragmentC和D位于选项卡2下.启动应用程序时,将显示片段A并选择选项卡1.然后Fragment可以用片段B替换A.当选择标签2时,应显示片段C. 如果选择了选项卡1,Fragment则应再次显示B. 此时,应该可以使用后退按钮显示片段A.
以下是fragment在同一选项卡中设置的代码:
Fragment selectedFragment = ItemsFragment.newInstance();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content, selectedFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
Run Code Online (Sandbox Code Playgroud) android android-fragments fragment-backstack bottomnavigationview
所以我有一个场景,其中我有 5 个带有底部导航的片段。
1. 主页 2. 收件箱 3. 搜索 4. 通知 5. 个人资料
因此,我有另一个名为 (BarcodeDetail) 的片段,我从 Home Fragment 导航到该片段。
(首页 -> 条码详情)
现在从 BarcodeDetail 导航到 Search Fragment
(条码详细信息 -> 搜索)
但现在如果我从 BottomNavigationView 选择 Home Fragment 它不会转到 Home Fragment。
它不会选择与搜索片段相同的当前片段。
(日志打印Search Fragment的名称)
navController.addOnDestinationChangedListener((controller, destination, bundle) -> {
Timber.d("Destination -> %s", destination.getDisplayName());
});
Run Code Online (Sandbox Code Playgroud)
private void setupBottomNavigation() {
NavHostFragment navHostFragment = (NavHostFragment)
getSupportFragmentManager().findFragmentById
(R.id.fragment_container_view);
if (navHostFragment != null) {
navController = navHostFragment
.getNavController();
NavigationUI.setupWithNavController(
binding.bottomNavigation, navController);
initDestinationListener();
}
}
Run Code Online (Sandbox Code Playgroud)
<?xml version="1.0" encoding="utf-8"?>
<menu …Run Code Online (Sandbox Code Playgroud) 我正在BottomNavigationView与导航组件一起使用。当显示片段不是根片段时,选项卡图标不会更新(选择)。
示例:
当我在带有片段 A(也是根片段)的Tab Home和带有片段 B(也是根片段)的Tab Star之间切换时,它工作正常。
但是,当我从Tab Home导航到另一个片段(例如片段 A2),然后点击Tab Star并再次返回Tab Home时,仍然在 中选择Tab StarBottomNavigationView。
版本运行良好2.4.0-alpha05,当我将其更新到2.5.0-alpha01.
构建.gradle(应用程序)
implementation "androidx.navigation:navigation-fragment-ktx:2.5.0-alpha01"
implementation "androidx.navigation:navigation-ui-ktx:2.5.0-alpha01"
implementation "androidx.navigation:navigation-dynamic-features-fragment:2.5.0-alpha01"
Run Code Online (Sandbox Code Playgroud)
构建.gradle(根)
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.5.0-alpha01"
Run Code Online (Sandbox Code Playgroud)
<navigation 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/graph"
app:startDestination="@id/fragmentA">
<fragment
android:id="@+id/fragmentA"
android:name="ui.test.FragmentA"
tools:layout="@layout/fragment_test"
android:label="FragmentA" >
<action
android:id="@+id/action_fragmentA_to_fragmentA2"
app:destination="@id/fragmentA2" />
</fragment>
<fragment
android:id="@+id/fragmentA2"
android:name="ui.test.FragmentA2"
tools:layout="@layout/fragment_test"
android:label="FragmentA2" />
<fragment
android:id="@+id/fragmentB"
android:name="ui.test.FragmentB"
tools:layout="@layout/fragment_test"
android:label="FragmentB" />
</navigation>
Run Code Online (Sandbox Code Playgroud)
菜单:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"> …Run Code Online (Sandbox Code Playgroud) 我为我的应用程序添加了底部导航视图,但我需要在活动之间而不是片段之间的底部导航视图,因此我已将此代码添加到Java以用于我的所有3个活动.
当我在手机中选择"秒"或"第三"时,所有内容都是正确的,但问题是突出显示第一项.
我需要突出显示我按下的项目.
我已经使用了片段,它工作得很好,但我仍然是使用片段的初学者所以我正在使用活动.
第一个活动代码是:
BottomNavigationView mBottomNavigation;
mBottomNavigation =(BottomNavigationView) findViewById(R.id.BottomNavigator);
mBottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.Nav_Second:
Intent Second= new Intent(First.this, Second.class);
startActivity(Second);
break;
case R.id.Nav_Third:
Intent Third= new Intent(First.this, Third.class);
startActivity(Third);
break;
}
return true;
}
});
}}
Run Code Online (Sandbox Code Playgroud)
第二项活动是:
BottomNavigationView mBottomNavigation;
mBottomNavigation =(BottomNavigationView) findViewById(R.id.BottomNavigator);
mBottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.Nav_First:
Intent First= new Intent(Second.this, First.class);
startActivity(First);
break;
case R.id.Nav_Third:
Intent Third= new Intent(Second.this, Third.class); …Run Code Online (Sandbox Code Playgroud) java android android-navigation bottomnavigationview android-bottomnav
我试图在创建的活动上设置默认项目,但它不起作用?这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_userhome);
mTextMessage = (TextView) findViewById(R.id.message);
profile = (FrameLayout) findViewById(R.id.profile);
mall = (FrameLayout) findViewById(R.id.mall);
dietplan =(FrameLayout) findViewById(R.id.dietplan);
BottomNavigationView navigation = (BottomNavigationView)
findViewById(R.id.navigation);
navigation.setSelectedItemId(R.id.dietplan);
navigation.setOnNavigationItemSelectedListener
(mOnNavigationItemSelectedListener);
}
Run Code Online (Sandbox Code Playgroud)
但似乎navigation.setSelectedItemId(R.id.dietplan);没有用.请帮我设置底部导航栏的默认项:
这是我的堆栈跟踪(logcat):
FATAL EXCEPTION: main
Process: gym.android.ommsoftware.gym, PID: 1915
java.lang.RuntimeException: Unable to start activity ComponentInfo{gym.android.ommsoftware.gym/gym.android.ommsoftware.gym.Userhome}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2404)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at …Run Code Online (Sandbox Code Playgroud)