【问题标题】:why my other view doesn't move up when the snackbar show after using coordinator layout?为什么我的其他视图在使用协调器布局后显示快餐栏时没有上移?
【发布时间】:2018-12-18 01:11:52
【问题描述】:

这是我的 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

我该怎么办?

【问题讨论】:

    标签: java android android-snackbar


    【解决方案1】:

    您不需要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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      • 2019-11-06
      相关资源
      最近更新 更多