代码之家  ›  专栏  ›  技术社区  ›  Alexa289

为什么在使用协调器布局后,当snackbar显示时,我的其他视图不会上移?

  •  0
  • Alexa289  · 技术社区  · 7 年前

    <?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>
    

    这是我的java文件:

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

    但我的按钮仍然粘在底部,当snackbar显示时不会向上移动。我希望我的按钮像视频中那样向上移动:

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

    我该怎么办?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Ankur Aggarwal    7 年前

    你不需要 LinearLayout

    请尝试以下操作:

    <?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>