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

后端 未结 4 470
灰色年华
灰色年华 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:26

    Do something like this:

    ViewGroup layout=new LinearLayout(context);
    TextView tv=new TextView(context); //your text
    tv.setText("my text"); 
    ImageView imageView=new ImageView(context); //your icon
    //filling image view with icon bitmap (in this case from resource)
    imageView.setImageBitmap(BitmapFactory.decodeStream(context.getResources().openRawResource(resourceId)));
    //ensuring that icon size will be more or less like text height
    imageView.setAdjustViewBounds(true);
    imageView.setMaxHeight((int )(tv.getLineHeight()*1.5));
    imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    layout.addView(imageView); //adding icon
    tv.setGravity(Gravity.BOTTOM|Gravity.LEFT);
    layout.addView(tv); //adding text
    

    Total idea is to create layout/viewgroup and add icon+text+whatever you want into viewgroup

提交回复
热议问题