📜  Android中的Snackbar材质设计组件

📅  最后修改于: 2021-05-13 15:12:46             🧑  作者: Mango

其他各种Material Design组件需要特殊的属性才能实现。但是在本文中,实现了Material design Snackbar,并且不需要特殊属性即可在应用程序中实现。请看以下图片,以区分普通小吃店和Android中的Material design Snackbar。 Material Design Snackbar的独特之处在于其设计,易于实现和定制。注意,我们将使用Java语言实现该项目。

Android中的Snackbar材质设计组件

实施材料设计快餐栏的步骤

第1步:创建一个空的活动Android Studio项目

  • 创建一个空的活动Android Studio项目。参考Android |如何在Android Studio中创建/启动新项目以创建Android Studio项目。注意,我们将使用Java语言实现该项目。

步骤2:添加所需的依赖项

  • 将Material design依赖库添加到应用程序级gradle文件。
  • 要获取应用程序级别的gradle文件,请转到“项目”>“ app”>“ build.gradle”
  • 并调用以下依赖项。
  • 如果无法获取应用程序级别的gradle文件并调用依赖项,请参考下图。调用依赖项后,单击右上角的“立即同步”按钮。并确保将系统连接到网络,以便可以下载所需的文件。

步骤3:将基本应用程序主题更改为styles.xml文件中的Material Components主题。

  • 要更改应用程序的基本主题,请转到app> src> res> styles.xml并调用以下代码。
XML

  
    
    
  


XML


  
    
    


Java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.material.snackbar.Snackbar;
  
public class MainActivity extends AppCompatActivity {
  
    // Button to show the snackbar
    Button bShowSnackbar;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // register the show snackbar button with the appropriate ID
        bShowSnackbar = findViewById(R.id.show_snackbar_button);
  
        // button click listener to show the snackbar
        bShowSnackbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar snackbar = Snackbar.make(v, "You have deleted an item", Snackbar.LENGTH_LONG);
                snackbar.setAction("UNDO", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // perform any action when the button on the snackbar is clicked here in this case 
                          // it shows a simple toast
                        Toast.makeText(MainActivity.this, "The item has been restored", Toast.LENGTH_SHORT).show();
                    }
                });
                snackbar.show();
            }
        });
    }
}


Java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.material.snackbar.Snackbar;
  
public class MainActivity extends AppCompatActivity {
  
    // Button to show the snackbar
    Button bShowSnackbar;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // register the show snackbar button with the appropriate ID
        bShowSnackbar = findViewById(R.id.show_snackbar_button);
  
        // button click listener to show the snackbar
        bShowSnackbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar snackbar = Snackbar.make(v, "You have deleted an item", Snackbar.LENGTH_LONG);
                snackbar.setAction("UNDO", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // perform any action when the button on the snackbar is clicked here in this case 
                          // it shows a simple toast
                        Toast.makeText(MainActivity.this, "The item has been restored", Toast.LENGTH_SHORT).show();
                    }
                });
                // the duration is in terms of milliseconds in this case its 3 seconds
                snackbar.setDuration(3000);
                snackbar.show();
            }
        });
    }
}


XML


  
    
    


Java
import androidx.appcompat.app.AppCompatActivity;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.material.snackbar.Snackbar;
  
public class MainActivity extends AppCompatActivity {
  
    // Button to show the snackbar
    Button bShowSnackbar;
  
    // coordinator layout for snackbar
    CoordinatorLayout mSnackbarLayout;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // register the show snackbar button with the appropriate ID
        bShowSnackbar = findViewById(R.id.show_snackbar_button);
  
        // register the coordinator layout with the appropriate ID
        mSnackbarLayout = findViewById(R.id.snackbar_layout);
  
        // button click listener to show the snackbar
        bShowSnackbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // pass the mSnackbarLayout as the view to the "make" function
                Snackbar snackbar = Snackbar.make(mSnackbarLayout, "You have deleted an item", Snackbar.LENGTH_LONG);
                snackbar.setAction("UNDO", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // perform any action when the button on the snackbar is clicked
                        // here in this case it shows a simple toast
                        Toast.makeText(MainActivity.this, "The item has been restored", Toast.LENGTH_SHORT).show();
                    }
                });
                // the duration is in terms of milliseconds
                snackbar.setDuration(3000);
                snackbar.show();
            }
        });
    }
}


XML


  
    
    


Java
import androidx.appcompat.app.AppCompatActivity;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.material.snackbar.Snackbar;
  
public class MainActivity extends AppCompatActivity {
  
    // Button to show the snackbar
    Button bShowSnackbar;
  
    // coordinator layout for snackbar
    CoordinatorLayout mSnackbarLayout;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // register the show snackbar button with the
        // appropriate ID
        bShowSnackbar = findViewById(R.id.show_snackbar_button);
  
        // register the coordinator layout with the
        // appropriate ID
        mSnackbarLayout = findViewById(R.id.snackbar_layout);
  
        // button click listener to show the snackbar
        bShowSnackbar.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // pass the mSnackbarLayout as the view
                        // to the make function
                        Snackbar snackbar = Snackbar.make(mSnackbarLayout, "You have deleted an item", Snackbar.LENGTH_LONG);
                        snackbar.setAction("UNDO", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                // perform any action when the button on the snackbar is clicked here in this
                                // case it shows a simple toast
                                Toast.makeText(MainActivity.this, "The item has been restored", Toast.LENGTH_SHORT).show();
                            }
                        });
                        // the duration is in terms of milliseconds
                        snackbar.setDuration(3000);
                        snackbar.show();
                    }
                });
    }
}


  • 如果无法找到和调用“材料组件”主题,请参考下图。

步骤4:现在使用activity_main.xml文件

  • 在activity_main.xml内调用以下XML代码,或者可以自行设计。

XML格式



  
    
    

输出UI:在模拟器上运行

Android中的Snackbar材质设计组件

步骤5:现在使用MainActivity。Java

Java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.material.snackbar.Snackbar;
  
public class MainActivity extends AppCompatActivity {
  
    // Button to show the snackbar
    Button bShowSnackbar;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // register the show snackbar button with the appropriate ID
        bShowSnackbar = findViewById(R.id.show_snackbar_button);
  
        // button click listener to show the snackbar
        bShowSnackbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar snackbar = Snackbar.make(v, "You have deleted an item", Snackbar.LENGTH_LONG);
                snackbar.setAction("UNDO", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // perform any action when the button on the snackbar is clicked here in this case 
                          // it shows a simple toast
                        Toast.makeText(MainActivity.this, "The item has been restored", Toast.LENGTH_SHORT).show();
                    }
                });
                snackbar.show();
            }
        });
    }
}

产生以下输出:

材料设计的更多功能

功能1:手动设置小吃栏的持续时间

  • MainActivity中调用以下代码。 Java的
  • 在这种情况下,Snackbar的关闭持续时间设置为3秒。

Java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.material.snackbar.Snackbar;
  
public class MainActivity extends AppCompatActivity {
  
    // Button to show the snackbar
    Button bShowSnackbar;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // register the show snackbar button with the appropriate ID
        bShowSnackbar = findViewById(R.id.show_snackbar_button);
  
        // button click listener to show the snackbar
        bShowSnackbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar snackbar = Snackbar.make(v, "You have deleted an item", Snackbar.LENGTH_LONG);
                snackbar.setAction("UNDO", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // perform any action when the button on the snackbar is clicked here in this case 
                          // it shows a simple toast
                        Toast.makeText(MainActivity.this, "The item has been restored", Toast.LENGTH_SHORT).show();
                    }
                });
                // the duration is in terms of milliseconds in this case its 3 seconds
                snackbar.setDuration(3000);
                snackbar.show();
            }
        });
    }
}

产生以下输出:

功能2:在FAB(浮动操作按钮)上防止Snackbar重叠

  • 要防止Sncakbar的简单重叠,请参阅Android中如何避免Snackbar重叠浮动操作按钮?此方法将acnhorPoint的设置显示为“浮动动作”按钮。
  • activity_main.xml中调用以下代码。

XML格式



  
    
    
  • 现在与MainActivity合作。 Java文件来处理Snackbar的重叠。

Java

import androidx.appcompat.app.AppCompatActivity;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.material.snackbar.Snackbar;
  
public class MainActivity extends AppCompatActivity {
  
    // Button to show the snackbar
    Button bShowSnackbar;
  
    // coordinator layout for snackbar
    CoordinatorLayout mSnackbarLayout;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // register the show snackbar button with the appropriate ID
        bShowSnackbar = findViewById(R.id.show_snackbar_button);
  
        // register the coordinator layout with the appropriate ID
        mSnackbarLayout = findViewById(R.id.snackbar_layout);
  
        // button click listener to show the snackbar
        bShowSnackbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // pass the mSnackbarLayout as the view to the "make" function
                Snackbar snackbar = Snackbar.make(mSnackbarLayout, "You have deleted an item", Snackbar.LENGTH_LONG);
                snackbar.setAction("UNDO", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // perform any action when the button on the snackbar is clicked
                        // here in this case it shows a simple toast
                        Toast.makeText(MainActivity.this, "The item has been restored", Toast.LENGTH_SHORT).show();
                    }
                });
                // the duration is in terms of milliseconds
                snackbar.setDuration(3000);
                snackbar.show();
            }
        });
    }
}

输出:在模拟器上运行

功能3:滑动功能可让Snackbar将其关闭

  • activity_main.xml中调用以下代码

XML格式



  
    
    
  • 现在与MainActivity合作。 Java文件并在构建Snackbar时,请确保传递“ make ”函数的协调器布局。

Java

import androidx.appcompat.app.AppCompatActivity;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.material.snackbar.Snackbar;
  
public class MainActivity extends AppCompatActivity {
  
    // Button to show the snackbar
    Button bShowSnackbar;
  
    // coordinator layout for snackbar
    CoordinatorLayout mSnackbarLayout;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // register the show snackbar button with the
        // appropriate ID
        bShowSnackbar = findViewById(R.id.show_snackbar_button);
  
        // register the coordinator layout with the
        // appropriate ID
        mSnackbarLayout = findViewById(R.id.snackbar_layout);
  
        // button click listener to show the snackbar
        bShowSnackbar.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // pass the mSnackbarLayout as the view
                        // to the make function
                        Snackbar snackbar = Snackbar.make(mSnackbarLayout, "You have deleted an item", Snackbar.LENGTH_LONG);
                        snackbar.setAction("UNDO", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                // perform any action when the button on the snackbar is clicked here in this
                                // case it shows a simple toast
                                Toast.makeText(MainActivity.this, "The item has been restored", Toast.LENGTH_SHORT).show();
                            }
                        });
                        // the duration is in terms of milliseconds
                        snackbar.setDuration(3000);
                        snackbar.show();
                    }
                });
    }
}

输出:在模拟器上运行

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处,前往由我们的专家精心策划的指南,以使您立即做好行业准备!