SORU
21 EYLÜL 2012, Cuma


Nasıl arka plan drawable programlı olarak Android yer

Arka Plan ayarlamak için:

RelativeLayout layout =(RelativeLayout)findViewById(R.id.background);
layout.setBackgroundResource(R.drawable.ready);

En iyi yolu yapmaktır?

CEVAP
21 EYLÜL 2012, Cuma


layout.setBackgroundResource(R.drawable.ready); doğru.
Bunu başarmak için başka bir yol kullanmak için aşağıdaki gibidir:

final int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    layout.setBackgroundDrawable( getResources().getDrawable(R.drawable.ready) );
} else {
    layout.setBackground( getResources().getDrawable(R.drawable.ready));
}

Ama sorun büyük görüntüleri yüklemek için çalışıyoruz çünkü ortaya bence.
Here büyük bitmap yüklemek için nasıl iyi bir öğretici olduğunu.

GÜNCELLEME:
getDrawable(int ) API level 22 kaldırılmış


getDrawable(int ) şimdi API level 22 önerilmiyor. Destek kitaplığı aşağıdaki kodu kullanmanız gerekir:

ContextCompat.getDrawable(context, R.drawable.ready)

Eğer ContextCompat.getDrawable, kaynak kodlarına bakın Eğer böyle bir şey verir:

/**
 * Return a drawable object associated with a particular resource ID.
 * <p>
 * Starting in {@link android.os.Build.VERSION_CODES#LOLLIPOP}, the returned
 * drawable will be styled for the specified Context's theme.
 *
 * @param id The desired resource identifier, as generated by the aapt tool.
 *            This integer encodes the package, type, and resource entry.
 *            The value 0 is an invalid identifier.
 * @return Drawable An object that can be used to draw this resource.
 */
public static final Drawable getDrawable(Context context, int id) {
    final int version = Build.VERSION.SDK_INT;
    if (version >= 21) {
        return ContextCompatApi21.getDrawable(context, id);
    } else {
        return context.getResources().getDrawable(id);
    }
}

ContextCompat daha fazla bilgi

Olarak getDrawable yerine getDrawable(int, Theme) yöntemi kullanmalısınız 22, (int) API.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Caroline Martin

    Caroline Mar

    19 EYLÜL 2008
  • CHISTOSITOJAJA

    CHISTOSITOJA

    27 HAZİRAN 2010
  • Christopher Bill

    Christopher

    30 NİSAN 2009