2012-01-28 49 views
0

晚報,調整警報圖標

我遇到了這個非常有用的帖子,我所擁有的一切,除了尋找漿紗好... 帖子:how to add icon in alert dialog before each item?

有大約的setBounds註釋,然後調用不同setCompoundDrawables。

這裏是我有什麼

... 
switch(currently_selected){ 
... 
    case 3: 

       final Item[] items2 = { 
        new Item("Company 1", R.drawable.c_1), 
        new Item("Company 2", R.drawable.c_2), 
        new Item("Company 3", R.drawable.c_3), 
        new Item("Company 4", R.drawable.c_4), 
        new Item("Company 5", R.drawable.c_5)//no icon for this one 
       }; 

       ListAdapter adapter = new ArrayAdapter<Item>(
               MainPortal.this, 
               android.R.layout.select_dialog_item, 
               android.R.id.text1, 
               items2){ 
         public View getView(int position, View convertView, ViewGroup parent) { 
          //User 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(items2[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; 
         } 
        }; 

       AlertDialog.Builder builder1 = new AlertDialog.Builder(MainPortal.this); 
       builder1.setTitle("Please select the Module you wish to load and use"); 
       builder1.setAdapter(adapter, new DialogInterface.OnClickListener() { 
       //builder1.setItems(items1, new DialogInterface.OnClickListener() { // comment out this line to try to use the adapter 
        public void onClick(DialogInterface dialog, int item) { 
         Toast.makeText(getApplicationContext(), items1[item], Toast.LENGTH_LONG).show(); 
        } 
       }).create().show(); 

       break; 
... 
}//end switch 

所以這個作品非常好,5-6小時後,我得到它的工作。

雖然我太新,在這瞭解一些這些東西都是。 我的公司1-5,可以選擇將公司標題加載到名稱旁邊。

我想這些規模下降,同時保持其縱橫比如果可能的話。 因此,

int max_allowed = 50; 
if(getWidth() > getHeight){ 
    scaleTo(50, (getWidth()/getHeight()*50)); 
}else if(getWidth() < getHeight){ 
    scaleTo((getWidth()/getHeight()*50), 50) ; 
}else{ 
    scaleTo(50, 50); 
} 

任何和朝向來設置邊界的正確方法幫助將不勝感激,我不理解items2 [位置] .icon爲整數並且引用或來自其轉換被拉伸。

感謝, 約書亞

回答

0

我設法找到挖一個公平位後的答案。

所需的變動情況,以調整如下。其中一個簡單的後if語句就足以調整公司的橫幅尺寸

final Drawable image; 
Resources res = getResources(); 
image = res.getDrawable(items2[position].icon); 
image.setBounds(0, 0, 50, 50); 
tv.setCompoundDrawables(image, null, null, null); 

//tv.setCompoundDrawablesWithIntrinsicBounds(image,0,0,0);//items2[position].icon, 0, 0, 0);