Java 使用底部导航栏
除了显示不同的字符串外,我如何使用底部导航栏(android支持小部件)进行其他操作?我的意思是,如何使用它在布局之间切换?没有找到解决方案,而且“Java 使用底部导航栏,java,android,android-layout,bottomnavigationview,Java,Android,Android Layout,Bottomnavigationview,除了显示不同的字符串外,我如何使用底部导航栏(android支持小部件)进行其他操作?我的意思是,如何使用它在布局之间切换?没有找到解决方案,而且“setContentView(R.layout.example);”不起作用 这里有一个快速入门的片段: public class MainActivity extends AppCompatActivity { private BottomBar mBottomBar; @Override protected void o
setContentView(R.layout.example);
”不起作用 这里有一个快速入门的片段:
public class MainActivity extends AppCompatActivity {
private BottomBar mBottomBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Notice how you don't use the setContentView method here! Just
// pass your layout to bottom bar, it will be taken care of.
// Everything will be just like you're used to.
mBottomBar = BottomBar.bind(this, R.layout.activity_main,
savedInstanceState);
mBottomBar.setItems(
new BottomBarTab(R.drawable.ic_recents, "Recents"),
new BottomBarTab(R.drawable.ic_favorites, "Favorites"),
new BottomBarTab(R.drawable.ic_nearby, "Nearby"),
new BottomBarTab(R.drawable.ic_friends, "Friends")
);
mBottomBar.setOnItemSelectedListener(new OnTabSelectedListener() {
@Override
public void onItemSelected(final int position) {
// the user selected a new tab
}
});
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mBottomBar.onSaveInstanceState(outState);
}
}
How to use ?
首先,我们需要更新我们的依赖关系
编译'com.android.support:design:25.0.0'
设计xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Content Container -->
<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="@color/white"
app:itemTextColor="@color/white"
app:menu="@menu/bottom_navigation_main" />
</RelativeLayout>
设置内容视图是整个活动,而不是底部栏。。。尝试使用
LayoutInflater
你在找这样的东西吗?如果某个组件未按预期工作,请将您的问题包括在代码中,因为该组件与所询问的组件不同@蟋蟀007非常感谢,但我已经尝试过这一款,但它不仅仅是在琴弦之间切换。这就引出了我的问题:它如何在多个字符串之间切换?“我现在是个彻头彻尾的傻瓜,所以我很高兴能得到任何帮助。@Belmandel我不知道你想做什么。”。你是想回复Euphor08吗?@Euphor08非常感谢你的帮助。我已经设置好了导航栏之类的东西,我需要知道的是如何让它在布局之间切换,因为从这些代码中可以看出,它不会在布局之间切换layouts@cricket_007不,我回应了你。我已经尝试了你发布的示例,但它不能在布局之间切换。。
<?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_favorites"
android:enabled="true"
android:icon="@drawable/ic_favorite_white_24dp"
android:title="@string/text_favorites"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_schedules"
android:enabled="true"
android:icon="@drawable/ic_access_time_white_24dp"
android:title="@string/text_schedules"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_music"
android:enabled="true"
android:icon="@drawable/ic_audiotrack_white_24dp"
android:title="@string/text_music"
app:showAsAction="ifRoom" />
</menu>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white" android:state_enabled="true" />
<item android:color="@color/colorPrimaryDark" android:state_enabled="false" />
</selector>
BottomNavigationView bottomNavigationView = (BottomNavigationView)
findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_favorites:
**question now is: what to put inside here so it switch to another layout??**
break;
case R.id.action_schedules:
**question now is: what to put inside here so it switch to another layout??**
break;
case R.id.action_music:
**question now is: what to put inside here so it switch to another layout??**
break;
}
return false;
}
});