我正在尝试从其他类别更改工具栏颜色,但始终失败。我不知道怎么了
我尝试使用LayoutInflater
来执行此操作,但仍然失败。您能帮我解决这个问题吗?
LoadColor.java
public class LoadColor {
private Context context;
private HomeActivity hA;
final String KEY_SAVED_RADIO_BUTTON_INDEX = "SAVED_RADIO_BUTTON_INDEX";
public LoadColor(Context context) {
this.context = context;
}
public void LoadPreferences(){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.activity_settings, null,false);
LayoutInflater tiup = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View homeAct = tiup.inflate(R.layout.activity_home, null,false);
Toolbar tb = (Toolbar) homeAct.findViewById(R.id.toolbarHome);
RadioGroup radioGroup = (RadioGroup) contentView.findViewById(R.id.radioSex);
SharedPreferences sharedPreferences = context.getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
int savedRadioIndex = sharedPreferences.getInt(KEY_SAVED_RADIO_BUTTON_INDEX, 0);
RadioButton savedCheckedRadioButton = (RadioButton) radioGroup.getChildAt(savedRadioIndex);
savedCheckedRadioButton.setChecked(true);
RadioGroup genderGroup = (RadioGroup) contentView.findViewById(R.id.radioSex);
RadioButton male = (RadioButton) contentView.findViewById(R.id.theme1);
RadioButton female = (RadioButton) contentView.findViewById(R.id.theme2);
if (genderGroup.getCheckedRadioButtonId() == -1) {
hA = new HomeActivity();
hA.setToolbarColor(tb, context.getResources().getColor(R.color.colorPrimary));
}
else {
if (male.isChecked()) { // one of the radio buttons is checked
hA = new HomeActivity();
hA.setToolbarColor(tb, context.getResources().getColor(R.color.colorPrimary));
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
((Activity) context).getWindow().setStatusBarColor(Color.parseColor("#014a53"));
}
}
else if (female.isChecked()) {
hA = new HomeActivity();
hA.setToolbarColor(tb, context.getResources().getColor(R.color.colorAccent));
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
((Activity) context).getWindow().setStatusBarColor(Color.parseColor("#db503d"));
}
}
}
}
}
activity_home.xml
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbarHome"
android:layout_width="match_parent"
android:layout_height="64dp"
app:popupTheme="@style/AppTheme.PopupOverlay"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
</android.support.design.widget.AppBarLayout>
HomeActivity.java
private LoadColor Lc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//toolbar logo and desc
Toolbar topToolBar = (Toolbar)findViewById(R.id.toolbarHome);
setSupportActionBar(topToolBar); //munculkan menu ke toolbar
topToolBar.setLogo(R.mipmap.ikon);
topToolBar.setLogoDescription(getResources().getString(R.string.logo_desc));
Lc = new LoadColor(this);
Lc.LoadPreferences();
} //OnCreate
public static void setToolbarColor(Toolbar toolbar, @ColorInt int color) {
toolbar.setBackgroundColor(color);
}
您可以在setStatusBarColor
中看到LoadColor.java
代码,但是在工具栏setBackgroundColor
中它无效。
答案 0 :(得分:0)
在onCreate内或要更改工具栏颜色的任何地方,都可以调用此静态实用程序函数,并将其传递给要更改其背景的工具栏的引用。当然,这是在您确定要为背景使用哪种颜色之后发生的。
//Tools.java
public static void setToolbarColor(Toolbar toolbar, @ColorInt int color) {
toolbar.setBackgroundColor(color);
}
例如:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//toolbar logo and desc
Toolbar topToolBar = (Toolbar)findViewById(R.id.toolbarHome);
setSupportActionBar(topToolBar); //munculkan menu ke toolbar
topToolBar.setLogo(R.mipmap.ikon);
topToolBar.setLogoDescription(getResources().getString(R.string.logo_desc));
//determine which color you want to use for the toolbar's background here
//you may use a local method to do that and return the resource value
//it can be an int resource or it can simply be a stored resource.
Tools.setToolbarColor(toolbar,getResources().getColor(R.color.colorPrimary));
//you can also parse the color from a string
setToolbarColor(topToolBar, Color.parse("RED"));
} //OnCreate