我一直在我的应用程序中实现一个底部导航栏,问题是无论我在仪表板图标上的哪个活动都是突出显示的。我怎样才能得到它,无论我在哪个活动中突出显示它?
public class Dashboard extends AppCompatActivity
implements View.OnClickListener
{
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener()
{
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item)
{
switch (item.getItemId())
{
case R.id.navigation_request:
Intent r = new Intent(Dashboard.this, Request.class);
startActivity(r);
finish();
break;
case R.id.navigation_settings:
Intent s = new Intent(Dashboard.this, AppSettings.class);
startActivity(s);
finish();
break;
}
return false;
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
这是我的XML菜单文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home" />
<item
android:id="@+id/navigation_request"
android:icon="@drawable/ic_request_icon"
android:title="@string/title_request" />
<item
android:id="@+id/navigation_settings"
android:icon="@drawable/ic_icon_settings"
android:title="@string/title_settings" />
</menu>
以下是我在activity_dashboard
中使用导航的方法<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!-- ONLY TEXTVIEWS ARE HERE -->
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation" />
</LinearLayout>
答案 0 :(得分:1)
步骤-1将以下代码放入应用程序级别Build.gradle
implementation 'androidx.navigation:navigation-fragment:2.3.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.navigation:navigation-ui:2.3.0'
步骤-2将以下代码放入activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
步骤-3将以下代码放入menu / bottom_nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home" />
<item
android:id="@+id/navigation_dashboard"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="Orders" />
<item
android:id="@+id/navigation_notifications"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="Services" />
<item
android:id="@+id/navigation_account"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="Account" />
</menu>
步骤-4将以下代码放入MainActivity.java
import android.os.Bundle;
import android.view.MenuItem;
import com.example.bottomnavigationdemo.ui.account.AccountFragment;
import com.example.bottomnavigationdemo.ui.dashboard.DashboardFragment;
import com.example.bottomnavigationdemo.ui.home.HomeFragment;
import com.example.bottomnavigationdemo.ui.notifications.NotificationsFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navView = findViewById(R.id.nav_view);
navView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
Fragment fragment = new HomeFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.nav_host_fragment, fragment, fragment.getClass().getSimpleName())
.commit();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.nav_host_fragment, fragment, fragment.getClass().getSimpleName());
transaction.commit();
return true;
case R.id.navigation_dashboard:
Fragment fragment2 = new DashboardFragment();
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.nav_host_fragment, fragment2, fragment2.getClass().getSimpleName());
transaction.commit();
return true;
case R.id.navigation_notifications:
Fragment fragment3 = new NotificationsFragment();
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.nav_host_fragment, fragment3, fragment3.getClass().getSimpleName());
transaction.commit();
return true;
case R.id.navigation_account:
Fragment fragment4 = new AccountFragment();
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.nav_host_fragment, fragment4, fragment4.getClass().getSimpleName());
transaction.commit();
return true;
}
return true;
}
});
}
}
步骤-5将以下代码放入HomeFragment.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import com.example.bottomnavigationdemo.R;
import com.example.bottomnavigationdemo.ui.dashboard.DashboardFragment;
public class HomeFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_home, container, false);
return root;
}
}
答案 1 :(得分:0)
只需使用方法setSelectedItemId(int id)
即可将您标记为选中的项目,就像点击它一样。
像这样:
BottomNavigationView bottomNavigationView;
bottomNavigationView = (BottomNavigationView)
findViewById(R.id.bottomNavigationView);
bottomNavigationView.setSelectedItemId(R.id.my_menu_item_id);
答案 2 :(得分:0)
建议不要使用许多不同的Activity
个实例,这些实例都会复制BottomNavigationView
及其相关代码(如OnNavigationItemSelectedListener
等)。相反,当用户与Activity
进行交互时,您通常会使用一个BottomNavigationView
来托管Fragment
以及.replace()
个BottomNavigationView
个BottomNavigationView
个实例。 {1}}。
话虽如此,让我们看看我们是否可以解决您的问题。
这个难题有两个部分。第一个很简单:您必须指明应该选择setSelectedItemId()
中的哪个项目。这可以通过调用onCreate()
来实现。在 navigation.setSelectedItemId(R.id.navigation_settings);
方法中添加类似的内容。
setSelectedItemId()
第二个稍微复杂一些。当您致电OnNavigationItemSelectedListener
时,系统的行为就像用户点击了该项目一样。换句话说,您的return false
将被触发。
看着你发布的听众,我注意到你总是onNavigationItemSelected()
。如果您查看true
的文档,则会找到
<强>返回强>:
false
将项目显示为所选项目,setSelectedItemId()
如果不应选择该项目
因此,true
的来电无需更改您的听众即可返回setSelectedItemId()
。
如果您在setOnNavigationItemSelectedListener()
来电之前拨打,那么您仍然可以通过true
来电解决问题,但这只是掩盖了问题。如果您希望点按的项目显示为选中状态,最好修复您的听众返回Ability
。