BottomSheetDialog
L'arrière-plan de/clignote lors du basculement entre les applications. Qu'est-ce que je fais mal ?
MainActivity.Java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn1).setOnClickListener(v -> {
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(MainActivity.this);
bottomSheetDialog.setContentView(R.layout.content);
bottomSheetDialog.show();
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<Android.support.constraint.ConstraintLayout
xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:app="http://schemas.Android.com/apk/res-auto"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
Android:id="@+id/btn1"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</Android.support.constraint.ConstraintLayout>
content.xml
<?xml version="1.0" encoding="utf-8"?>
<View xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:app="http://schemas.Android.com/apk/res-auto"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="300dp"
Android:background="#F00" />
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:Android="http://schemas.Android.com/apk/res/Android"
package="com.holtaf.testandroidapplication">
<application
Android:allowBackup="true"
Android:label="@string/app_name"
Android:supportsRtl="true"
Android:theme="@style/Theme.AppCompat.Light.DarkActionBar">
<activity Android:name=".MainActivity">
<intent-filter>
<action Android:name="Android.intent.action.MAIN" />
<category Android:name="Android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Ma version appcompat
est 27.1.1.
La principale raison du scintillement est due au style par défaut de BottomSheetDialog
qui définit un comportement par défaut animation et dim .
Nous pouvons résoudre le problème ci-dessus en définissant un thème personnalisé à l'aide de bottomSheetDialogTheme
qui à son tour:
@null
.false
.Exemple:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="bottomSheetDialogTheme">@style/AppTheme.BottomSheetDialog</item>
</style>
<style name="AppTheme.BottomSheetDialog" parent="Theme.Design.BottomSheetDialog">
<item name="Android:windowAnimationStyle">@null</item>
<item name="Android:backgroundDimEnabled">false</item>
<!-- optional -->
<item name="Android:windowBackground">#99323232</item>
</style>
Utilisez la disposition Coordonnée au lieu de la disposition de contrainte et définissez la disposition de la feuille de fond au format XML comme ci-dessous.
<Android.support.design.widget.CoordinatorLayout
xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:app="http://schemas.Android.com/apk/res-auto"
Android:layout_width="match_parent"
Android:layout_height="match_parent">
<Button
Android:id="@+id/btn1"
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:text="Hello World!"
Android:layout_gravity="center" />
<Android.support.v4.widget.NestedScrollView
xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:app="http://schemas.Android.com/apk/res-auto"
Android:id="@+id/bottom_sheet_map"
Android:layout_width="match_parent"
Android:layout_height="150dp"
app:layout_behavior="Android.support.design.widget.BottomSheetBehavior">
<include layout="@layout/layout_bottom_sheet" />
</Android.support.v4.widget.NestedScrollView>
</Android.support.design.widget.CoordinatorLayout>
En classe Java Utilisez comme ceci.
private BottomSheetBehavior mBottomSheetBehavior;
private View bottomSheet;
private isBottomSheetExpand = false;
...
btn1.setOnClickListner(new View.OncliView.OnClickListener(){
@Override
public void onClick(View v) {
if(isBottomSheetExpand){
openBottomSheet();
}else{
closeBottomSheet();
}
}
});
...
public void closeBottomSheet() {
if (mBottomSheetBehavior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
isBottomSheetExpand = false;
}
}
public void openBottomSheet() {
if (mBottomSheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED) {
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
isBottomSheetExpand = true;
}
}
Selon documentation , la méthode setPeekHeight est responsable du comportement des fenêtres pop-up. En utilisant le
bottomSheet.setState(STATE_HIDDEN) should do the trick.