簡體   English   中英

為什么在使用協調器布局后顯示快餐欄時,我的其他視圖沒有上移?

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

所以這是我的xml文件

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


            }
        });
    }
}

但是我的按鈕仍然停留在底部,並且在小吃店顯示時不會向上移動。 我希望我的按鈕像下面的視頻一樣向上移動:

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

我該怎么辦 ?

您不需要LinearLayout 同樣,您不需要放置約束,因為它的協調器布局而不是ConstraintLayout。

嘗試以下方法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM