问题
The Material design website mentions a new Toast-like element called a Snackbar: http://www.google.com/design/spec/components/snackbars-and-toasts.html
The Android L preview SDK documentation (sorry can't link since it's only downloadable) doesn't have any mention of Snackbar in the classes list or as a modifier in the Toast class documentation. Am I missing something obvious or should I build my own Snackbar.java?
回答1:
Update 2015-05-29:
Google released a Design Support Library which includes a Snackbar and other Material Design widgets.
The Snackbar lib mentioned in the original answer is now deprecated.
Original answer
I'm sure Google will eventually include it in a future SDK, along with a Floating Action Button that is also missing in the preview SDK.
As @friedrich nietzche pointed out, I implemented a library to include a Snackbar
in your project.
https://github.com/nispok/snackbar
Hope it helps!
回答2:
FWIW,
It would appear that there is no Snackbar
implementation in the L Developer Preview. I've also implemented a Snackbar library with the intentions of being as close to the material design guidelines as I can. Thanks.
回答3:
Mabye take a look at this here. http://www.williammora.com/2014/08/snackbar-android-library.html
I am guessing the native version will show up in the sdk eventually. It is a bit odd I agree.
回答4:
Snackbar is effectively just a Crouton with some margins. Crouton in its current form only supports adding to start (0th item) of a ViewGroup, however you can find the very "strayan" enhancement to Crouton, DownUnderMode, at my github. Just beware that the official Crouton library and DownUnderMode version are a little out of sync (which will hopefully be fixed in the year 2058 when the DownUnderMode pull request is accepted).
回答5:
Using design library we can implement for all the versions of Android 2.1 onward.
Here is the working example code http://www.feelzdroid.com/2015/06/snackbar-android-example-using-design-support-library.html.
If you need any help, drop comment.
回答6:
Here is simple way to implement snackbar in android
Step 1. Add support library 23 and compile your project with
compile 'com.android.support:appcompat-v7:23.0.1'
Step 2. Add coordinate layout in your activity file
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</android.support.design.widget.CoordinatorLayout>
Step 3. Now add following code in your MainActivity.java to implement snackbar
public class MainActivity extends AppCompatActivity {
CoordinatorLayout coordinatorLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
ShowSnack();
}
public void ShowSnack() {
Snackbar snackbar = Snackbar.make(coordinatorLayout, "Snackbar Label", Snackbar.LENGTH_LONG);
snackbar.setAction("Action", new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Action", Toast.LENGTH_LONG).show();
}
});
snackbar.setActionTextColor(Color.RED);
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(Color.DKGRAY);
TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.YELLOW);
snackbar.show();
}
}
Hope this will work for you.
For more android tutorial please follow this blog: Trinity Tuts
来源:https://stackoverflow.com/questions/25633695/is-there-code-for-snackbars-in-android-l-or-are-we-expected-to-implement-them-ou