2010-10-13 21 views
31

Tôi đang sử dụng AlertDialog (xem mã bên dưới) và muốn đặt hình ảnh trước mỗi văn bản.Cách thêm biểu tượng trước mỗi mục trong hộp thoại cảnh báo?

Ví dụ, biểu tượng email sau đó văn bản "Email", Facebook icon sau đó văn bản "Facebook", vv

Sử dụng đoạn mã sau, làm thế nào để thêm một biểu tượng trước mỗi giá trị văn bản?

final CharSequence[] items = { "Email", "Facebook", "Twitter", "LinkedIn" }; 
AlertDialog.Builder builder = new AlertDialog.Builder(More.this); 
builder.setTitle("Share Appliction"); 
builder.setItems(items, new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int item) { 
     if (item == 0) { 

     } else if (item == 1) { 

     } else if (item == 2) { 

     } else if(item == 3) { 

     } 
    } 
}); 
AlertDialog alert = builder.create(); 
alert.show(); 
+0

http://stackoverflow.com/questions/7792931/how-to-design-single-selection-dialog – UMAR

Trả lời

2

Làm như thế này:

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 

Tổng số ý tưởng là để tạo ra bố cục/ViewGroup và thêm biểu tượng dấu + văn bản + bất cứ điều gì bạn muốn vào ViewGroup

+0

cách để kết hợp nó với cảnh báo? – UMAR

+0

AlertDialog thường chứa ListView hiển thị tin nhắn. Vì vậy, hãy thử để có được như AlertDialog.getListView(); Và sau đó thêm bố trí của bạn có chứa toàn bộ công cụ với Icon/Text thêm vào listView. như: AlertDialog.getListview() Thêm (layout) – barmaley

77

Bạn phải sử dụng một tùy chỉnh ListAdapter thêm hình ảnh của bạn. Trên đường đi là phân lớp ArrayAdapter (được sử dụng theo mặc định bởi AlertDialog). Dưới đây là một ví dụ:

final Item[] items = { 
    new Item("Email", android.R.drawable.ic_menu_add), 
    new Item("Facebook", android.R.drawable.ic_menu_delete), 
    new Item("...", 0),//no icon for this one 
}; 

ListAdapter adapter = new ArrayAdapter<Item>(
    this, 
    android.R.layout.select_dialog_item, 
    android.R.id.text1, 
    items){ 
     public View getView(int position, View convertView, ViewGroup parent) { 
      //Use super class to create the View 
      View v = super.getView(position, convertView, parent); 
      TextView tv = (TextView)v.findViewById(android.R.id.text1); 

      //Put the image on the TextView 
      tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0); 

      //Add margin between image and text (support various screen densities) 
      int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f); 
      tv.setCompoundDrawablePadding(dp5); 

      return v; 
     } 
    }; 


new AlertDialog.Builder(this) 
    .setTitle("Share Appliction") 
    .setAdapter(adapter, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int item) { 
      //... 
     } 
    }).show(); 

đây là lớp hàng

public static class Item{ 
    public final String text; 
    public final int icon; 
    public Item(String text, Integer icon) { 
     this.text = text; 
     this.icon = icon; 
    } 
    @Override 
    public String toString() { 
     return text; 
    } 
} 
+1

Có cách nào để thiết lập kích thước có thể vẽ không? – Muzikant

+1

Đừng bận tâm, chỉ cần tìm hiểu cách thực hiện ... gọi setBounds với kích thước cần thiết và sau đó gọi setCompoundDrawables thay vì setCompoundDrawablesWithIntrinsicBounds – Muzikant

+0

Xin chào, công trình này nhưng kích thước của văn bản của một mục không phải là mặc định - nó quá lớn . Bất kỳ suy nghĩ làm thế nào tôi có thể đặt nó vào kích thước văn bản mặc định? – Nedko

1

Để mở rộng hình ảnh:

//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); 
1

Tôi thích giải pháp Tom Esterez 's nhưng khuyên bạn sử dụng các chức năng tương đối thay vì cho RTL ủng hộ.

Vì vậy, sử dụng này:

tv.setCompoundDrawablesRelativeWithIntrinsicBounds(items[position].iconID, 0, 0, 0); 

thay vì điều này:

tv.setCompoundDrawablesWithIntrinsicBounds(items[position].iconID, 0, 0, 0); 
Các vấn đề liên quan