您可以简单地创建自己的 Snackbar 类并模拟 Snackbar 的 make 方法。这样做,你只需要使用这个类而不是android的snackbar小部件。
Snackbar.class
import android.graphics.Color;
import android.support.annotation.IntDef;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.view.View;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
public class Snackbar {
/** Snackbar's lengths **/
public static final int LENGTH_SHORT = android.support.design.widget.Snackbar.LENGTH_SHORT;
public static final int LENGTH_LONG = android.support.design.widget.Snackbar.LENGTH_LONG;
public static final int LENGTH_INDEFINITE = android.support.design.widget.Snackbar.LENGTH_INDEFINITE;
@NonNull
public static android.support.design.widget.Snackbar make(@NonNull View view, @NonNull CharSequence text,
@Duration int duration) {
android.support.design.widget.Snackbar snackbar = android.support.design.widget.Snackbar.make(view, text, duration);
// TODO: This is where you have to customize your snackbar
snackbar.getView().setBackgroundColor(Color.RED);
return snackbar;
}
@NonNull
public static android.support.design.widget.Snackbar make(@NonNull View view, @StringRes int resId, @Duration int duration) {
return make(view, view.getResources().getText(resId), duration);
}
// Optional
@IntDef({LENGTH_INDEFINITE, LENGTH_SHORT, LENGTH_LONG})
@IntRange(from = 1)
@Retention(RetentionPolicy.SOURCE)
public @interface Duration {}
}
利用:
// WARNING: Make sure you're using your snackbar's package
import com.mypackage.custom_views.Snackbar;
public class MyActivity extends Activity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
Snackbar.make(view, R.string.my_msg, Snackbar.LENGTH_LONG).show();
}
}
希望这可以帮助!