答案可能为时已晚,但我希望它能对其他人有所帮助。
与其使用外部库,不如创建自己的布局并使用它。
第 1 步:为选项创建布局。
image_choose_bottom_sheet.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvClickImageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_vector_camera_icon"
android:gravity="center"
android:padding="10dp"
android:text="Take Photo"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="@+id/tvChooseGalleryText"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvChooseGalleryText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_vector_gallery_icon"
android:gravity="center"
android:padding="10dp"
android:text="Choose Image"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/tvClickImageText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/tvClickImageText"
app:layout_constraintTop_toTopOf="@+id/tvClickImageText" />
</android.support.constraint.ConstraintLayout>
</layout>
第 2 步:创建一个 BottomSheetDialog,您可以在其中扩展布局并进行配置。
DialogImageChooser.java
public class DialogImageChooser extends BottomSheetDialogFragment {
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
View view;
// DialogNationalitySelectionBinding mBinding;
Context mContext;
private bottomSheetListener mListener;
TextView tvClickImageText, tvChooseGalleryText;
public DialogImageChooser() {
}
@SuppressLint("ValidFragment")
public DialogImageChooser(@NonNull Context context, bottomSheetListener mListener) {
this.mListener = mListener;
mContext = context;
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = inflater.inflate(R.layout.image_choose_bottom_sheet, container, false);
tvClickImageText = view.findViewById(R.id.tvClickImageText);
tvChooseGalleryText = view.findViewById(R.id.tvChooseGalleryText);
tvClickImageText.setOnClickListener(v -> {
mListener.onCardClicked("camera");
dismiss();
});
tvChooseGalleryText.setOnClickListener(v -> {
mListener.onCardClicked("gallery");
dismiss();
});
return view;
}
public interface bottomSheetListener {
void onCardClicked(String option);
}
}
第 3 步:最后在 Activity 中调用 Dialog。
YourActivity.java
ivItemImage.setOnClickListener(v -> {
new DialogImageChooser(YourActivity.this, selectedOption -> {
if (selectedOption.equalsIgnoreCase("camera")) {
TakePictureIntent(CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
} else if (selectedOption.equalsIgnoreCase("gallery")) {
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECT_IMAGE);
}
}).show(getSupportFragmentManager(), "add_photo_dialog");
});
就是这样。无需使用任何外部库即可集成自定义“从图库或相机对话框中选择图像”。