Set style for TextView programmatically

后端 未结 12 1923
南方客
南方客 2020-11-22 09:47

I\'m trying to use the TextView constructor with style like this:

TextView myText = new TextView(MyActivity.this, null, R.style.my_style);
         


        
12条回答
  •  广开言路
    2020-11-22 10:27

    I do not believe you can set the style programatically. To get around this you can create a template layout xml file with the style assigned, for example in res/layout create tvtemplate.xml as with the following content:

    
    
    

    then inflate this to instantiate your new TextView:

    TextView myText = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null);
    

    Hope this helps.

提交回复
热议问题