How to add an icon before each item in alert dialog?

后端 未结 4 459
灰色年华
灰色年华 2020-12-02 10:15

I am using an AlertDialog (see the below code) and would like to put an image before each text.

For example, email icon then text \"Email\", Facebook icon then text

4条回答
  •  [愿得一人]
    2020-12-02 10:47

    To scale images:

    //Put the image on the TextView
    int dp50 = (int) (50 * getResources().getDisplayMetrics().density + 0.5f);
    Drawable dr = getResources().getDrawable(R...YOUR_DRAWABLE);
    Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
    Drawable d = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, dp50, dp50, true));
    tv.setCompoundDrawablesWithIntrinsicBounds(d, null, null, null);
    
    // Add margin between image and text (support various screen densities)
    int dp10 = (int) (10 * getResources().getDisplayMetrics().density + 0.5f);
    tv.setCompoundDrawablePadding(dp10);
    

提交回复
热议问题