簡體   English   中英

無法更改其他類別的工具欄顏色

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

我正在嘗試從其他類更改工具欄顏色,但始終失敗。 我不知道怎么了

我嘗試使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM