在 DrawerLayout 中以编程方式更改浮动操作按钮图像
Change Floating Action Button Image Programmatically in DrawerLayout
正如标题所示,当用户从 DrawerLayout 打开特定片段时,我试图以编程方式更改浮动图像按钮的 icon/image。这是我已经尝试过的:首先,这是 DrawerLayout class.
的 onCreate() 方法
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_drawer);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), TransaksiKeranjang.class);
startActivity(intent);
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_transaksi, R.id.nav_pesanan, R.id.nav_sejarah, R.id.nav_inventory,
R.id.nav_akun, R.id.nav_cabang, R.id.nav_laporan, R.id.nav_logout)
.setDrawerLayout(drawer)
.build();
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
//Change fab icon when branch fragment open
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
if (destination.getId() == R.id.nav_cabang) {
fab.setImageDrawable(ContextCompat.getDrawable(getParent(), R.drawable.icon_add));
}
}
});
navigationView.getMenu().findItem(R.id.nav_logout).setOnMenuItemClickListener(menuItem -> {
Intent intent = new Intent(MenuDrawer.this, Login.class);
startActivity(intent);
return true;
});
}
然后这是存在浮动操作按钮的 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"
android:background="@drawable/background_fragment"
tools:context=".MenuDrawer">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.HuwenakApp.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/huwenak_brown"
app:popupTheme="@style/Theme.HuwenakApp.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/icon_cart" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
问题是当我尝试更改 fab 图像时,我总是在这一行中收到 nullPointerException 错误:
fab.setImageDrawable(ContextCompat.getDrawable(getParent(), R.drawable.icon_add));
这很奇怪,因为 onClickListener 工作正常并且没有给我这个错误。
您必须添加上下文
fab.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.icon_add))
您在文档中看到 getDrawable 需要上下文,如果您传递了父项,则会出现以下错误
尝试在空对象引用上调用虚方法'android.graphics.drawable.Drawable android.content.Context.getDrawable(int)'
getDrawable(@NonNull Context context, @DrawableRes int id)
正如标题所示,当用户从 DrawerLayout 打开特定片段时,我试图以编程方式更改浮动图像按钮的 icon/image。这是我已经尝试过的:首先,这是 DrawerLayout class.
的 onCreate() 方法protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_drawer);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), TransaksiKeranjang.class);
startActivity(intent);
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_transaksi, R.id.nav_pesanan, R.id.nav_sejarah, R.id.nav_inventory,
R.id.nav_akun, R.id.nav_cabang, R.id.nav_laporan, R.id.nav_logout)
.setDrawerLayout(drawer)
.build();
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
//Change fab icon when branch fragment open
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
if (destination.getId() == R.id.nav_cabang) {
fab.setImageDrawable(ContextCompat.getDrawable(getParent(), R.drawable.icon_add));
}
}
});
navigationView.getMenu().findItem(R.id.nav_logout).setOnMenuItemClickListener(menuItem -> {
Intent intent = new Intent(MenuDrawer.this, Login.class);
startActivity(intent);
return true;
});
}
然后这是存在浮动操作按钮的 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"
android:background="@drawable/background_fragment"
tools:context=".MenuDrawer">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.HuwenakApp.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/huwenak_brown"
app:popupTheme="@style/Theme.HuwenakApp.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/icon_cart" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
问题是当我尝试更改 fab 图像时,我总是在这一行中收到 nullPointerException 错误:
fab.setImageDrawable(ContextCompat.getDrawable(getParent(), R.drawable.icon_add));
这很奇怪,因为 onClickListener 工作正常并且没有给我这个错误。
您必须添加上下文
fab.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.icon_add))
您在文档中看到 getDrawable 需要上下文,如果您传递了父项,则会出现以下错误
尝试在空对象引用上调用虚方法'android.graphics.drawable.Drawable android.content.Context.getDrawable(int)'
getDrawable(@NonNull Context context, @DrawableRes int id)