开发者

how to translate XML layout background attribute value into code?

开发者 https://www.devze.com 2023-03-24 04:57 出处:网络
I\'m sure this is really easy, but one of those hard to find things for beginners!Here\'s wha开发者_开发百科t I have in my layout XML file which I want to play around with programatically:

I'm sure this is really easy, but one of those hard to find things for beginners! Here's wha开发者_开发百科t I have in my layout XML file which I want to play around with programatically:

<View 
    android:layout_width="fill_parent" 
    android:layout_height="1dp" 
    android:background="?android:attr/listDivider" 
/> 

And here is how far I've gotten with the code:

View ruler = new View(this);
ruler.setBackgroundDrawable(????); //or is it setBackgroundResource?
parent.addView(ruler, 
               new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 1));

I'm stuck on how to translate "?android:attr/listDivider" into code and which setter on the View object I'm interested in. Can anyone help with this? Thanks!


It is said here (in the "XML attributes" array) that android:background is equivalent to setBackgroundResource.

I guess you should try .setBackgroundResource(android.R.attr.listDivider).

0

精彩评论

暂无评论...
验证码 换一张
取 消