【发布时间】:2020-01-23 01:31:07
【问题描述】:
有什么办法可以改变Fragment的ActionBar颜色,将ActionBar中显示的标题改成被按下的菜单选项的标题?
【问题讨论】:
标签: android android-fragments android-actionbar android-actionbaractivity
有什么办法可以改变Fragment的ActionBar颜色,将ActionBar中显示的标题改成被按下的菜单选项的标题?
【问题讨论】:
标签: android android-fragments android-actionbar android-actionbaractivity
如果您使用的是 AppCompat,则必须始终调用 getSupportActionBar()
更改标题:
getSupportActionBar().setTitle("Your Title");
改变颜色:
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.YOUR_COLOR));
如果在片段中:
更改标题:
getActivity().getActionBar().setTitle("YOUR TITLE");
改变颜色:
getActivity().getAcationBar().setBackgroundDrawable(new ColorDrawable(Color.YOUR_COLOR));
希望对你有帮助
【讨论】:
getActivity()之后使用getActionBar()
这是用java编写的建议答案:
getActionBar().setTitle("Test");
getActionBar().setBackgroundDrawable(new ColorDrawable("COLOR"));
【讨论】:
如果您使用支持库:
android.support.v7.app.ActionBar supportActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (supportActionBar != null) {
supportActionBar.setTitle("My action bar title");
View view = new View(getContext());
view.setBackgroundColor(getResources().getColor(R.color.my_color));
supportActionBar.setBackgroundDrawable(view.getBackground());
}
否则:
android.app.ActionBar actionBar = getActivity().getActionBar();
if (actionBar != null) {
actionBar.setTitle("My action bar title");
View view = new View(getContext());
view.setBackgroundColor(getResources().getColor(R.color.my_color));
actionBar.setBackgroundDrawable(view.getBackground());
}
【讨论】: