第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Android 如何在所有活動(dòng)中動(dòng)態(tài)更改主題?

Android 如何在所有活動(dòng)中動(dòng)態(tài)更改主題?

開心每一天1111 2021-05-31 13:35:39
我正在嘗試在所有活動(dòng)中動(dòng)態(tài)切換背景主題。但是目前,我只能讓它打開開關(guān)所在的當(dāng)前活動(dòng)。我嘗試了以下方法:使用setTheme于其他活動(dòng)集的主題,但導(dǎo)致的錯(cuò)誤:cannot find symbol method setTheme(int):ConstraintLayout homeActivity = (ConstraintLayout) findViewById(R.id.homeLayout);homeActivity.setTheme(R.style.darktheme);也試過了getTheme,但是報(bào)錯(cuò)non-static method getTheme() cannot be referenced from a static context::Resources.Theme theme = super.getTheme();theme.applyStyle(R.style.darktheme,true);請?zhí)峁┯嘘P(guān)如何修復(fù)這些錯(cuò)誤或替代方法的指導(dǎo)。更新,首選項(xiàng)管理器遇到的新問題:這是當(dāng)前實(shí)現(xiàn)的,當(dāng)切換黑暗主題時(shí)應(yīng)該跨活動(dòng)顯示,但應(yīng)用程序完全變黑而沒有錯(cuò)誤:myswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {        @Override        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {            if (isChecked){                PreferenceManager                        .getDefaultSharedPreferences(DisplaySettingActivity.this)                        .edit()                        .putInt("ActivityTheme", R.style.darktheme)                        .commit();                DisplaySettingActivity.this.recreate();            }
查看完整描述

2 回答

?
飲歌長嘯

TA貢獻(xiàn)1951條經(jīng)驗(yàn) 獲得超3個(gè)贊

您可以嘗試創(chuàng)建一個(gè)改變主題的 baseActivity,然后子類化該活動(dòng)并修改您的主題。讓我知道它是否有效


查看完整回答
反對(duì) 回復(fù) 2021-06-02
?
鴻蒙傳說

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊

為了擴(kuò)展 Saham 的答案,這可以通過子類化 Activity 并覆蓋 onCreate() 來解決:


public class ThemeChangeActivity extends Activity {


    @Override

    public void onCreate(Bundle savedInstanceState) {

        // Gets the saved theme ID from SharedPrefs, 

        // or uses default_theme if no theme ID has been saved

        int theme = PreferenceManager.getDefaultSharedPreferences(this).getInt("ActivityTheme", R.id.default_theme);

        // Set this Activity's theme to the saved theme

        setTheme(theme);

        // Continue with creation

        super.onCreate(savedInstanceState);

    }

}

現(xiàn)在,從 ThemeChangeActivity 擴(kuò)展所有要更改主題的活動(dòng)。每當(dāng)您想更改活動(dòng)的主題時(shí),您都可以使用:


PreferenceManager

    .getDefaultSharedPreferences(this)

    .edit()

    .putInt("ActivityTheme", R.id.theme_you_want_to_set)

    .commit();


查看完整回答
反對(duì) 回復(fù) 2021-06-02
  • 2 回答
  • 0 關(guān)注
  • 171 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)