我的问题是动态添加后如何设置textView样式。
代码如下:
LinearLayout layout = (LinearLayout)findViewById(R.id.linarLay);
TextView textView = new TextView(this);
textView.setText("TEST1");
layout.addView(textView);
我可以看到已添加的 TextView ,但是..我现在需要设置它的样式.. 到目前为止,我试过这个:
textView.setTextAppearance(getApplicationContext(),R.style.textStyle);
我在 layout.addView(textView); 之后试过这段代码;之前它只是一样不会改变任何事情..
任何想法/解决方案将不胜感激......谢谢
最佳答案
样式不会改变,因为尽管您使用相同的 TextView
对象在将其添加到 Layout
后设置样式,但它不是布局的一部分。您必须添加 View
,从布局使用它的 id,当您更改其样式时,它会直接影响您在 Layout
上的 View 。
试试这个:(我没有测试过,但*应该可以)
LinearLayout layout = (LinearLayout)findViewById(R.id.linarLay);
TextView textView = new TextView(this);
textView.setText("TEST1");
textView.setId(999); // give some id
layout.addView(textView);
TextView tv=(TextView)findViewById(999);
tv.setTextAppearance(getApplicationContext(),R.style.textStyle);
关于android - 动态添加后的Textview样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12494554/