utilisation de base
- créer un constructeur
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.myAlertDialog);
- Créer et injecter une vue
Afficher la vue = LayoutInflater.from(this).inflate(R.layout.dialog_note_color_tips, null);
builder.setView(vue);
…
对view进行操作
…
- exposition
Boîte de dialogue AlertDialog=builder.create();
dialogue .afficher
utilisation de la mise à niveau
- Ajoutez le paramètre de position de la boîte à puces
et appelez-le après le spectacle
Fenêtre de dialogueWindow = mNotesColorTipsDialog.getWindow();
dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.gravity = Gravity.CENTER ;
lp.largeur = 400 ;
dialogWindow.setAttributes(lp);
- Ajouter des animations
dialogWindow.setWindowAnimations(R.style.dialogWindowAnimUp);
Le code global est le suivant :
if (mNotesColorTipsDialog == null) {
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.myAlertDialog);
View view = LayoutInflater.from(this).inflate(R.layout.dialog_note_color_tips, null);
builder.setView(view);
builder.setCancelable(false);
mNotesColorTipsDialog = builder.create();
Button button = view.findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mNotesColorTipsDialog.dismiss();
}
});
checkBox = view.findViewById(R.id.cb);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferencesHelper.getSpInstance(mContext).put(Constants.NOTE_COLOR_TIP_SHOW, !isChecked);
if (isChecked) {
checkBox.setTextColor(getResources().getColor(R.color.purple));
} else {
checkBox.setTextColor(getResources().getColor(R.color.gray));
}
}
});
}
checkBox.setChecked(!show);
mNotesColorTipsDialog.show();
Window dialogWindow = mNotesColorTipsDialog.getWindow();
dialogWindow.setWindowAnimations(R.style.dialogWindowAnimUp);
dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.gravity = Gravity.CENTER;
lp.width = 400;
dialogWindow.setAttributes(lp);
interpolateur d'augmentation d'animation
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/anticipate_interpolator">
<translate
android:duration="1000"
android:fromYDelta="0"
android:toYDelta="-100%" />
</set>
Présentation de plusieurs interpolateurs :
classe java | identifiant de ressource xml | illustrer |
---|---|---|
AccélérerDécélérerInterpolateur | @android:mind/accelerate_decelerate_interpolator | Ses changements commencent et se terminent à un rythme lent et s'accélèrent au milieu |
AccélérerInterpolateur | @android:mind/accelerate_interpolator | Son changement commence à un rythme lent puis s'accélère |
Interpolateur de décélération | @android:mind/decelerate_interpolator | Son changement commence à un rythme plus rapide puis ralentit |
InterpolateurLinéaire | @android:mind/linear_interpolator | Son taux de variation est constant |
AnticiperInterpolateur | @android:mind/anticipate_interpolator | Sa variation commence par lancer en arrière, puis en avant |
AnticipateOvershootInterpolator | @android:anim/anticipate_overshoot_interpolator | Sa variation commence à reculer, puis à avancer, dépasse la valeur cible et revient finalement à la valeur finale |
OvershootInterpolator | @android:anim/overshoot_interpolator | Sa variation commence à s'envoler vers l'avant, dépasse la valeur cible et revient finalement à la valeur finale |
BounceInterpolator | @android:anim/bounce_interpolator | son changement rebondit à la fin |
CycleInterpolator | @android:mind/cycle_interpolator | Jouer en boucle avec un rythme sinusoïdal |
TimeInterpolator | Une interface, vous pouvez personnaliser l'interpolateur |
Deux étapes pour réaliser le fond transparent des coins arrondis de la bullet box
- Définissez le style transparent et transmettez-le lors de la création pour obtenir un arrière-plan transparent
<style name="myAlertDialog" parent="Theme.MaterialComponents.Dialog.Alert">
<item name="android:background">@android:color/transparent</item>
</style>
- Ajoutez ensuite un arrière-plan arrondi au fichier de mise en page
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@drawable/bg_corner"//设置圆角背景
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content">