gpt4 book ai didi

java - 如何更改片段内的汉堡颜色?

转载 作者:行者123 更新时间:2023-12-01 16:35:35 25 4
gpt4 key购买 nike

我想更改我的主页片段中汉堡包的颜色。

我的主要 Activity 代码是

public class HomeActivity extends AppCompatActivity {

private AppBarConfiguration mAppBarConfiguration;
ActionBarDrawerToggle actionBarDrawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);

actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.open_drawer, R.string.close_drawer);

//actionBarDrawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.colorAccent));

// 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_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.home, menu);
return true;
}

@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}

最佳答案

创建公共(public)函数。在您的 Activity 中。

private AppBarConfiguration mAppBarConfiguration;
ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

//.....
}

public void setToolbarIconColor(){
Drawable drawable = toolbar.getNavigationIcon();
int tint = Color.parseColor("#FF0000"); // color red
PorterDuff.Mode mode = PorterDuff.Mode.SRC_ATOP;
drawable.setColorFilter(tint,mode);
toolbar.setNavigationIcon(drawable);
}

并调用 Activity 函数。如你所愿

public class HomeFragment extends Fragment {

//.....

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((HomeActivity) getActivity()).setToolbarIconColor();
}
}

关于java - 如何更改片段内的汉堡颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61954302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com