How to add custom item in android Theme declaration?

后端 未结 1 1999
谎友^
谎友^ 2021-02-05 17:56

I\'m having few custom themes in my styles.xml
Now whenever the activity takes the theme, it uses the colorPrimary, colorPrimaryDark and <

1条回答
  •  悲&欢浪女
    2021-02-05 18:25

    Create a attrs.xml file shown in image.

    
    
    
       
       
    
    
    

    customTheme 1

    
    

    customTheme 2

    
    

    Setting Color to TextView as example.

    You can use it in similar way in any widget anywhere.

    This TextView is used in below activity.

    
    

    Want to set theme dynamically.

    public class AboutUsActivity extends Activity {
    
        int theme = 1;
        // int theme = 2;  2nd theme.
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            switch (theme) {
                default:
                case 1:
                    this.setTheme(R.style.customTheme1);
                    break;
                case 2:
                    this.setTheme(R.style.customTheme2);
                    break;
    
            }
            // you must call `setTheme()` before `setContentView()`
            setContentView(R.layout.activity_about);
    
        }
    

    For multiple activities you have set theme for each of them separately.

    0 讨论(0)
提交回复
热议问题