Android Studio - 设置图标并更改字体颜色

时间:2016-03-01 11:39:00

标签: android android-studio

在我的应用中,我添加了代码,允许用户从图库中选择图像或使用相机拍照。当在右上角选择摄像机图标时会出现这种情况,并出现一个包含这两个选项的对话框。我只是想知道如何添加一个可用作按钮的图标。因此,例如当对话框出现时,我想要一个android画廊图标的图像,当你选择它时会带你进入画廊。还想要文字'画廊'用不同的字体颜色。

以下是用于创建选项的代码

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;
}

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

解决方案是创建一个活动,列出您图库中的所有图片(因为这是您自己的活动,您可以使用任何图标或字体)。除此之外,您无法更改默认图库应用程序

答案 2 :(得分: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();
        }
    });

注意:以上代码仅适用于图库按钮,以类似的方式为相机按钮执行。