簡體   English   中英

以編程方式在Android中更改顏色值

[英]Change color value in style programmatically android

美好的一天,我有這樣的風格:

<style name="ViewPagerTitleStrip">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">#FFFFFF</item>
    <item name="android:textSize">14sp</item>
</style>

我想在此方法中更改textColor:

void setFontColor(){
    SharedPreferences mPref = this.getSharedPreferences(Constants.USER_SETTINGS_PREF, Context.MODE_PRIVATE);
    boolean colorPref = mPref.contains(Constants.USER_FONT_COLOR);
    if(colorPref){
        int color = mPref.getInt(Constants.USER_FONT_COLOR, -1);
        //Somewhere here
    }
}

我該如何實現呢?

您不能在運行時更改XML資源。

但是,您可以從SharedPreferences中獲取顏色值,並將其直接應用於視圖。

例如,假設您的視圖具有setTextColor()方法:

if (colorPref) {
    int color = mPref.getInt(Constants.USER_FONT_COLOR, -1);
    titleStrip.setTextColor(color);
}

暫無
暫無

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

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