当我按返回键时,底部导航的颜色不会改变,但是我的片段会改变。我想一次更改两者。也就是说,当我返回时,片段应随底部导航图标一起更改。
这是我当前的代码:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private FrameLayout mMainFrame;
Fragment homeFragment = new HomeFragment();
Fragment trendingFragment = new TrendingFragment();
Fragment latestFragment = new LatestFragment();
Fragment inboxFragment= new InboxFragment();
Fragment libraryFragment = new LibraryFragment();
// Adding acion on botom navigation icon basically adding Fragment Action
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.bottom_navigation_home:
setFragment(homeFragment);
return true;
case R.id.bottom_navigation_trending:
setFragment(trendingFragment);
return true;
case R.id.bottom_navigation_latest:
setFragment(latestFragment);
return true;
case R.id.bottom_navigation_inbox:
setFragment(inboxFragment);
return true;
case R.id.bottom_navigation_library:
setFragment(libraryFragment);
return true;
default:
return false;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Decleration Connecting Java To xml
mMainFrame = (FrameLayout) findViewById(R.id.main_container);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.bottom_navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
setFragment(homeFragment); // Start Home Fregment first
}
@Override
public void onBackPressed(){
if (getSupportFragmentManager().getBackStackEntryCount() == 1){
finish();
}
else {
super.onBackPressed();
}
}
//Declear Method
private void setFragment(Fragment fragment) {
// Set fragment in frame layout
String backStateName = fragment.getClass().getName();
String fragmentTag = backStateName;
FragmentManager manager = getSupportFragmentManager();
boolean fragmentPopped = manager.popBackStackImmediate (backStateName, 0);
if (!fragmentPopped && manager.findFragmentByTag(fragmentTag) == null){ //fragment not in back stack, create it.
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.main_container, fragment, fragmentTag);
ft.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK);
ft.addToBackStack(backStateName);
ft.commit();
}
}
}
bottom_nav_color_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorPrimary" android:state_checked="true"/>
<item android:color="@color/colorIcon" android:state_checked="false"/>
</selector>
MainActivity.xml
中的BottomNavigationView<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="@color/colorWhite"
android:clickable="true"
android:focusable="true"
app:itemIconSize="25dp"
app:itemIconTint="@color/bottom_nav_color_selector"
app:itemTextAppearanceActive="@style/BottomNavigationView.Active"
app:itemTextAppearanceInactive="@style/BottomNavigationView"
app:itemTextColor="@color/bottom_nav_color_selector"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_navigation" />
答案 0 :(得分:0)
您好,尝试将此setIconTint和itemTextColor设置为您的特定颜色的颜色选择器xml
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/Black"
app:itemBackground="@color/White"
app:itemIconTint="@color/nav_selector"
app:itemTextColor="@color/nav_selector"
app:menu="@menu/nav_menu" />
这是nav_selector.xml,将此文件放在res-> color文件夹中
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/OrangeBrown" android:state_checked="true" />
<item android:color="@color/OrangeBrown" android:state_enabled="true" android:state_pressed="true" />
<item android:color="@color/BlackishGray" />
</selector>
如果您想单击特定选项卡,则可以使用以下代码进行操作
View view = bottomNavigationView.findViewById(R.id.nav_home);
view.performClick();
答案 1 :(得分:0)
这里是ans。按下并激活导航按钮,可以完美地处理底部导航。
public class MainActivity extends AppCompatActivity {
private Fragment homeFragment = new HomeFragment();
private Fragment trendingFragment = new TrendingFragment();
private Fragment latestFragment = new LatestFragment();
private Fragment inboxFragment = new InboxFragment();
private Fragment libraryFragment = new LibraryFragment();
private BottomNavigationView navigation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navigation = findViewById(R.id.bottom_navigation);
}
@Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
finish();
} else {
super.onBackPressed();
if (homeFragment.isResumed())
navigation.setSelectedItemId(R.id.bottom_navigation_home);
else if (trendingFragment.isResumed())
navigation.setSelectedItemId(R.id.bottom_navigation_trending);
else if (latestFragment.isResumed())
navigation.setSelectedItemId(R.id.bottom_navigation_latest);
else if (inboxFragment.isResumed())
navigation.setSelectedItemId(R.id.bottom_navigation_inbox);
else if (libraryFragment.isResumed())
navigation.setSelectedItemId(R.id.bottom_navigation_library);
}
}