在类的代码中设置drawableLeft. [英] Set drawableLeft in code in a class.
问题描述
你好,我的好朋友,
当我尝试像这样在我的android应用程序中设置drawableLeft时:
Hello my good friends,
When I try to set a drawableLeft in my android application like this:
TextView textView = new TextView() findViewById(R.id.mainList);
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.mul, 0, 0, 0);
第一行出现错误:
构造函数TextView()未定义."
和
对于类型Item,未定义方法findViewById(int)."
我该如何解决?我正在上课.
谢谢!
保持甜蜜!
更新:
基本上,我的问题是:如何从不同的类(例如,在我的主活动中)访问TextView并设置drawableLeft(或例如文本)?
I get a error with the first line:
"The constructer TextView() is undefined."
and
"The method findViewById(int) is undefined for the type Item."
How can I solve this? I am working in a class.
Thanks!
Stay sweet!
Update:
Basicly my question is: How can I access a TextView and set a drawableLeft (or for example text) in for example my Main Activity from a diffrent class?
推荐答案
文档:
TextView.setCompoundDrawablesWithIntrinsicBounds(int,int,int,int) [ ] @ developer.android.com/reference/android [
The doc:
TextView.setCompoundDrawablesWithIntrinsicBounds(int, int, int, int)[] @ developer.android.com/reference/android[^]
so your call of the method seems to be fine. Have you set a number value for the callR.drawable.mul
? That one might not be there when you request it.
You can also use a variable first and set that one into your code to check the value while debugging:
TextView textView = new TextView() findViewById(R.id.mainList);
int iLeft = R.drawable.mul;
textView.setCompoundDrawablesWithIntrinsicBounds(iLeft, 0, 0, 0);
其他想法:何时将视图添加到应用程序?当您尝试绘制边界时它已经存在吗?您应该以图形方式更新视图吗?
Other idea: When is the view added to the app? is it already existing when you''re trying to draw the bound? should you update the view (graphically)?
这篇关于在类的代码中设置drawableLeft.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!