简体   繁体   English

无法更改其他类别的工具栏颜色

[英]Can't Change Toolbar Color From Different Class

I am trying to change toolbar color from different class, but always failed. 我正在尝试从其他类更改工具栏颜色,但始终失败。 I don't know, what's wrong? 我不知道怎么了

I've tried to do this using LayoutInflater but still failed. 我尝试使用LayoutInflater进行此操作,但仍然失败。 Can you help me to solve this issue? 您能帮我解决这个问题吗?

LoadColor.java 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 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 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);
    }

You can see the setStatusBarColor code in LoadColor.java it's work, but in the toolbar setBackgroundColor it doesn't work. 你可以看到setStatusBarColor代码LoadColor.java它的工作,但在工具栏setBackgroundColor这是行不通的。

Inside your onCreate or wherever you want to change the toolbar color you may call this static utility function and pass it the reference of the toolbar for which you want to change the background. 在onCreate内或要更改工具栏颜色的任何地方,都可以调用此静态实用程序函数,并将其传递给要更改背景的工具栏的引用。 This of course happens after you have identified which color you want to use for the background. 当然,这是在您确定要为背景使用哪种颜色之后发生的。

//Tools.java
public static void setToolbarColor(Toolbar toolbar, @ColorInt int color) {
    toolbar.setBackgroundColor(color);
}

For example: 例如:

@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM