2016-03-01 187 views
0

在我的應用程序中,我添加了代碼以允許用戶從圖庫中選擇圖像或使用相機拍攝照片。在右上角選擇攝像機圖標時出現這種情況,並出現一個對話框,其中包含這兩個選項。我只是想知道我將如何添加一個圖標,將用作按鈕。因此,例如,當對話框出現時,我想要一個android畫廊圖標的圖像,當您選擇將您帶入畫廊。還希望使用不同字體顏色的文字「圖庫」。Android Studio - 設置圖標並更改字體顏色

下面是用於創建選項

public boolean onOptionsItemSelected(MenuItem item) { 
    if (item.getItemId() == R.id.launch_voip_call) { 
     Utils.startCall(this, contact); 
     return true; 
    } else if (item.getItemId() == R.id.launch_camera) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage("Pick Image from") 
       .setPositiveButton("Camera", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         //camera intent 
         Intent cameraIntent = new Intent(ConversationActivity.this, CameraActivity.class); 
         cameraIntent.putExtra("EXTRA_CONTACT_JID", contact.getJid()); 
         startActivity(cameraIntent); 
        } 
       }) 
       .setNegativeButton("Gallery", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         Intent intent = new Intent(); 
         // Show only images, no videos or anything else 
         intent.setType("image/*"); 
         intent.setAction(Intent.ACTION_GET_CONTENT); 
         // Always show the chooser (if there are multiple options available) 
         startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST); 
        } 
       }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 
    } 
    return false; 
} 
+0

你想在畫廊屏幕上顯示你自己的圖庫和字體嗎? –

+0

是的,我想顯示自己的圖標,目前爲了進入畫廊...您選擇當前只是文本的'圖庫',在彈出選項框中選擇攝像機圖標時。所以,而不是文字,我想我自己的畫廊圖標出現。謝謝 –

+0

使用onShow事件:http://stackoverflow.com/a/20830973/1129995 – Zaki

回答

0

解決方案代碼是創建活動,將列出從您的畫廊所有圖像(因爲它是你自己的活動,你可以有任何圖標或字體)。除此之外,你水溼更改默認相冊應用

+0

這是實現這個目標的唯一可能嗎?謝謝 –

+0

是的,這是唯一達到此目的但我會建議你選擇一個已編寫的Github庫來顯示圖庫(只需根據您的字體和其他要求進行更改),而不是自己寫整個東西 –

+0

林不知道什麼你的意思是,我對android studio和編程很陌生。對不起 –

0

下面是部分代碼從圖庫中選擇圖片或採取與相機的幫助下,代碼...

你可以看到完整的在https://gist.github.com/Mariovc/f06e70ebe8ca52fbbbe2

public static Intent getPickImageIntent(Context context){ 
    Intent chooserIntent = null; 
    List<Intent> intentList = new ArrayList<>(); 
    Intent pickIntent = new Intent(Intent.ACTION_PICK, 
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    takePhotoIntent.putExtra("return-data", true); 
    takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(context))); 
    intentList = addIntentsToList(context, intentList, pickIntent); 
    intentList = addIntentsToList(context, intentList, takePhotoIntent); 
    if (intentList.size() > 0) { 
     chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1), 
       context.getString(R.string.pick_image_intent_text)); 
     chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{})); 
    } 
    return chooserIntent; 
} 
private static List<Intent> addIntentsToList(Context context, List<Intent> list, Intent intent) { 
    List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(intent, 0); 
    for (ResolveInfo resolveInfo : resInfo) { 
     String packageName = resolveInfo.activityInfo.packageName; 
     Intent targetedIntent = new Intent(intent); 
     targetedIntent.setPackage(packageName); 
     list.add(targetedIntent); 
    } 
    return list; 
} 
0

AlertDialog alert = builder.create();

dialog.setOnShowListener(new OnShowListener() { 

     @Override 
     public void onShow(DialogInterface dialogInterface) { 
      Button button = dialog.getButton(AlertDialog.BUTTON_NEGATIVE); 

      button.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.your_gallery_resource, 


      Drawable drawable = getActivity().getResources().getDrawable(
        android.R.drawable.your_gallery_resource); 

      // set the bounds to place the drawable a bit right 
      drawable.setBounds((int) (drawable.getIntrinsicWidth() * 0.5), 
        0, (int) (drawable.getIntrinsicWidth() * 1.5), 
        drawable.getIntrinsicHeight()); 
      button.setCompoundDrawables(drawable, null, null, null); 

      // could modify the placement more here if desired 
      // button.setCompoundDrawablePadding(); 
     } 
    }); 

注意下面給出的行地址:上面的代碼只對庫按鈕,做一個類似的方式進行相機按鈕。