background="@drawabletop/blue"求帮助

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
EDIT: Sorry i realize from your comment my question was not clear enough. I will post a new one. Sorry for this and thanks for your answers
I am populating a listview from a Json file.
With my listadapter, I can easily assign appropriate json data to each row of my list. That works well for text, for example:
TextView tv = (TextView)ll.findViewById(R.id.descView);
tv.setText(i.desc);
With the above code, every row will be correctly populated by the good json data.
However, I don't manage to do the same thing for an image. I have tried to set the right image from my json data using this:
ImageView iv = (ImageView)ll.findViewById(R.id.imgView);
iv.setBackgroundDrawable(context.getResources().getDrawable(i.img));
I guess I am doing something wrong with the type of my parameters: "setBackgroundDrawable" requires a drawable parameter.
"getDrawable" requires an int.
I have set the type of my field img to int, but that doesn't work.
Any idea why?
My list adapter:
public class adapter extends ArrayAdapter&ListItems& {
//Initialize adapter
public ListItemsAdapter(Context context, int resource, List&ListItems& items) {
super(context, resource, items);
this.resource=
public View getView(int position, View convertView, ViewGroup parent)
//Get the current object
ListItems i = getItem(position);
//Inflate the view
if(convertView==null)
ll = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
li = (LayoutInflater)getContext().getSystemService(inflater);
li.inflate(resource, ll, true);
ll = (LinearLayout) convertV
//For the message
TextView tv = (TextView)ll.findViewById(R.id.descView);
tv.setText(i.desc);
// For the Img
ImageView iv = (ImageView)ll.findViewById(R.id.imgView);
iv.setBackgroundDrawable(context.getResources().getDrawable(i.img));
my item class:
public class ListItems{
And a sample of my json file:
[{"id":10001,"img":e1,"desc":"desc1"},
{"id":10002,"img":e2,"desc":"desc2"},
{"id":10003,"img":e3,"desc":"desc3"}]
iv.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.img));
iv.setBackgroundResource(R.drawable.img);
6,93223973
id for a drawable have to be from R.drawable.lalala
if you want to load external images use
something like
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Stack Exchange
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabledStack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
If we use a ListView or an ExpandableListView the default background is @android:drawable/list_selector_background. I have an ExpandableListView which shows data grouped by date. I like the state list list_selector_background drawable and want to keep the behaviour for different states. But for the weekend (for some of the list view items) i'd like to define a custom background. But these items should still use the drawables for different states defined through list_selector_background.
I saw different not suitable answers. Most of them say i should define my own state list drawable.
The list_selector_background.xml defines, besides different drawable for different states, &item android:state_window_focused="false" android:drawable="@color/transparent" /&
So i thought i define my own state list drawable and just change the transparent to my desired color. The problem is the drawables used for the states are not public. I really want to stay close to the android style so i don't want to write my own state list drawable with own drawables for states.
At the end i wrote a solution using a layer list drawable.
&?xml version="1.0" encoding="utf-8"?&
&layer-list xmlns:android="/apk/res/android"&
android:drawable="@color/DarkBlue"
android:id="@+id/background" /&
android:drawable="@android:drawable/list_selector_background"
android:id="@+id/list_selector_background" /&
&/layer-list&
This perfectly matches the desired behaviour with one exception :/. I have my custom background and the item reacts with drawable defined in list_selector_background for press events.
But somehow the focused state does not use the drawable defined by list_selector_background (&item android:state_focused="true" android:drawable="@drawable/list_selector_background_focus" /&)
So i kindly ask if anyone can imagine why the focused state does not work? I am developing with API level 8. If you need any further information i'd like to post. Thanks
Know someone who can answer?
Share a link to this
via , , , or .
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Stack Exchange
Post as a guest
Post as a guest
By posting your answer, you agree to the
Browse other questions tagged
Stack Overflow works best with JavaScript enabledStack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
I am styling a Button whose width can change in the program through different Content. I got the png files from the designer like this:
[Left Border] [Background (just a strip with the gradient)] [Right Border]
I thought I'm fine with assigning the Properties of the Button like this:
Background: Background.png
Drawable left: LeftBorder.png
Drawable right: RightBorder.png
BUT it sha) Right now the Background.png begins where the Drawable Left begins and ends at the end of Drawable Right. Is using those Drawable Properties not the right way in this case?
Here is the Code:
android:text="@+id/Button01"
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/top_loaction_left"
android:drawableRight="@drawable/top_loaction_right"
android:background="@drawable/top_loaction_bg"&&/Button&
6,863114788
What you need is a 9 patch image.
5,90362143
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Stack Exchange
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabledStack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
I have next code in my custom view that extends EditText:
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int count = getLineCount();
Canvas cvs= new Canvas();
Drawable dr = this.getBackground();
Rect r = mR
Paint paint = mP
mTextPaint.setTextSize(this.getTextSize());
Paint PaintText = mTextP
for (int i = 0; i & i++) {
int baseline = getLineBounds(i, r);
cvs.drawText(Integer.toString(i+1), r.left, baseline + 1, PaintText);
cvs.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
cvs.drawLine(PaintText.measureText(Integer.toString(count)), this.getTop(), PaintText.measureText(Integer.toString(count)), this.getBottom(), paint);
dr.setBounds(0, 0, this.getRight(), this.getBottom());
dr.draw(cvs);
this.setBackgroundDrawable(dr);
Why is there nothing on backgroind of this view?
2,48752037
2,07431748
protected void onDraw(Canvas canvas) {
Bitmap bitmap = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.ARGB_8888);
int count = getLineCount();
Canvas cvs= new Canvas(bitmap);
cvs.drawColor(Color.rgb(245, 245, 245));
Rect r = mR
Paint paint = mP
mTextPaint.setTextSize(this.getTextSize());
Paint PaintText = mTextP
for (int i = 0; i & i++) {
int baseline = getLineBounds(i, r);
cvs.drawText(Integer.toString(i+1), 2, baseline + 1, PaintText);
cvs.drawLine(2, baseline + 1, r.right, baseline + 1, paint);
cvs.drawLine(PaintText.measureText(Integer.toString(count))+3, this.getTop(), PaintText.measureText(Integer.toString(count))+3, this.getBottom(), paint);
this.setBackgroundDrawable(new BitmapDrawable(getContext().getResources(), bitmap));
this.setPadding((int) (PaintText.measureText(Integer.toString(count))+7),3,3,3);
super.onDraw(canvas);
This code works as expected. Thank you very much Pixie.
2,07431748
I think you're trying to do something that can be done easier. However, your code doesn't work because:
You don't set a Bitmap for your Canvas object.
I think that you want to move content of the Canvas to the Drawable but in fact your code performs an opposite action. Actually, you draw the Drawable on the Canvas object. Try this:
Bitmap bitmap = Bitmap.createBitmap(this.getWidth(), this.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas cvs = new Canvas(bitmap);
// Your drawing stuff without dr.setBounds() and dr.draw()
this.setBackgroundDrawable(
new BitmapDrawable(getContext().getResources(), bitmap));
18.2k65265
You do not need to create a new Canvas object. onDraw creates one for you and sets it in its own bitmap. Just use the name of the canvas specified in the parameters (in this case, canvas).
onDraw is called everytime your view is created or invalidate() is called. And in your onDraw() method, you are creating a NEW canvas object. If you are using this code for anything graphically intense (like a game) then you are leaking memory. Even if you only draw your view once, this is not the best way to do so. Use the canvas provided in the onDraw() parameters.
1,28731227
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Stack Exchange
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled

我要回帖

更多关于 bitmap转drawable 的文章

 

随机推荐