简体   繁体   中英

why my other view doesn't move up when the snackbar show after using coordinator layout?

so here is my xml file

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/myCoordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <Button
            android:id="@+id/button"
            android:layout_width="168dp"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:text="Button"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />


    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

and here is my java file:

public class MainActivity extends AppCompatActivity {

    Button myButton;
    Snackbar mySnackbar;
    CoordinatorLayout myCoordinatorLayout;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myButton = (Button) findViewById(R.id.button);
        myCoordinatorLayout = (CoordinatorLayout) findViewById(R.id.myCoordinatorLayout);

        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mySnackbar = Snackbar.make(myCoordinatorLayout,"Test Snakcbar",BaseTransientBottomBar.LENGTH_INDEFINITE);
                mySnackbar.setDuration(8000);

                mySnackbar.setAction("OK", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mySnackbar.dismiss();
                    }
                });

                mySnackbar.show();


            }
        });
    }
}

but my button still stick in the bottom and doesn't move up when the snackbar show. I want my button move up like in this video:

https://developer.android.com/images/training/snackbar/snackbar_button_move.mp4

what should I do ?

You don't need the LinearLayout . Also, you don't need to put constraints, since its coordinator layout and not ConstraintLayout.

Try this instead:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:id="@+id/myCoordinatorLayout"
     android:layout_width="match_parent"
     android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="168dp"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:text="Button" />



</android.support.design.widget.CoordinatorLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM