سؤال

هل من الممكن تغيير السحب على زر عبر جافا؟

مثال؛ لدي زر مثل هذا.

<Button 
style="@style/Buttons" 
android:id="@+id/butFavTeam"
android:drawableBottom="@drawable/venue"/>

أريد تغيير صورة DrawableBottom الحالية مع آخر من دليل الرسم الخاص بي.

هل كانت مفيدة؟

المحلول

هل نظرت إلى هذه الطريقة:

/*
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.
*/
public void  setCompoundDrawablesWithIntrinsicBounds  (Drawable left, Drawable top, Drawable right, Drawable bottom)

جرب هذا:

/*remember to first clear the callback of the drawable you are replacing to prevent memory leaks...
* Get the Drawables for the button by calling: myButton.getCompoundDrawables(), then loop through that calling myOldDrawable.setCallback(null);
* e.g:*/

for(Drawable myOldDrawable : myButton.getCompoundDrawables())
{
   myOldDrawable.setCallback(null);
}

//use null where you don't want a drawable
myButton.setCompoundDrawablesWithIntrinsicBounds(null,null,null, myNewDrawable);

أتمنى أن يساعد ذلك.

نصائح أخرى

فقط وجدت ذلك.

Drawable myIcon = this.getResources().getDrawable(R.drawable.myVenue);
butFavTeam.setCompoundDrawablesWithIntrinsicBounds(null, null, null, myIcon);

بالمناسبة ... تأكد من عدم استخدام Method SetCompoundDrawables () عن طريق الخطأ. يتطلب الأمر نفس المعلمات مثل SetCompoundDrawableswithinTrinsicBounds () ، ولكن من الواضح أنه يتوقع بعض تفاصيل الحدود أيضًا.

لقد سقطت فريسة لهذا لأنني قبلت بتكاسل طريقة أول نوع من الأفعال. أقوم بنشر هذا في حال لم تتمكن من معرفة سبب عرض الصور.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top