开发者

Getting ListView drawables and applying it manually

i am trying to apply the style that a list item has, when it is selected, to a View (In my case a TextView)

Example

here it would be the orange-painted style. I found that this look is defined by a Drawable Object. So i tried, so get the drawable and applied it to my view

Tex开发者_运维问答tView tv = new TextView(this);     

tv.setText("Hello, Android");
tv.setBackgroundDrawable(new ListView(this).getSelector());
setContentView(tv);

but it doesn't work. Does anybody have an idea how to do that?


Ok I figured out how to do that. I found out hat ListView uses the ColorStateList list_selector_background which is defined in android.R.drawable. Using ResourceBrowser I got to know that the fourth color is the selected drawable so i added the following to my code

StateListDrawable listDrawables= (StateListDrawable)getResources().getDrawable(android.R.drawable.list_selector_background);
listDrawables.selectDrawable(3);
Drawable highlightDrawable = listDrawables.getCurrent();

Now I can set my Background using higlightDrawable. I don't know if it is possible to access the drawable in xml I did not try it yet. Thanks for help!


It would be a pain to do it by code.

You need to implement a selector and let android know which resource needs to be used when the view is pressed, enabled or focused.

Here is sample example on how to achieve the same.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜