gpt4 book ai didi

java - 无法更改不同类别的工具栏颜色

转载 作者:行者123 更新时间:2023-12-02 01:46:02 28 4
gpt4 key购买 nike

我试图更改不同类别的工具栏颜色,但总是失败。我不知道,出了什么问题?

我尝试使用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它不起作用。

最佳答案

在 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

关于java - 无法更改不同类别的工具栏颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57454266/

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