标签:
使用Snackbar要导入com.android.support:design库。
compile ‘com.android.support:design:22.2.0’
Snackbar像Toast差不多。反正你看了也不会点赞,。。。直接上代码
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private CoordinatorLayout coordinatorLayout;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinator);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar snackbar = Snackbar.make(coordinatorLayout, "妹子拉黑了你!", Snackbar.LENGTH_LONG).setAction("再发一次", new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "我是单身狗", Toast.LENGTH_SHORT).show();
}
});
setSnackbarColor(snackbar,getResources().getColor(R.color.red),getResources().getColor(R.color.bule));
SnackbarAddView(snackbar, R.layout.icon2, 0);
SnackbarAddView(snackbar,R.layout.icon1,2);
snackbar.show();
}
});
}
/**
* 更还颜色和背景
* @param snackbar
* @param messageColor
* @param backgroundColor
*/
public static void setSnackbarColor(Snackbar snackbar, int messageColor, int backgroundColor) {
//获取Snackbar的view
View view = snackbar.getView();
if(view!=null){
//修改view的背景色
view.setBackgroundColor(backgroundColor);
//获取Snackbar的message控件,修改字体颜色
((TextView) view.findViewById(R.id.snackbar_text))
.setTextColor(messageColor);
((TextView)view.findViewById(R.id.snackbar_action)).setTextColor(Color.BLACK);//右边文字的颜色
}
}
/**
* 加入图标
* @param snackbar
* @param layoutId
* @param index
*/
public static void SnackbarAddView(Snackbar snackbar,int layoutId,int index) {
//获取snackbar的View(其实就是SnackbarLayout)
View snackbarview = snackbar.getView();
//将获取的View转换成
Snackbar.SnackbarLayout snackbarLayout=(Snackbar.SnackbarLayout)snackbarview;
View add_view = LayoutInflater.from(snackbarview.getContext())
.inflate(layoutId,null);//加载布局文件新建View
//设置新建布局参数
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT );
//设置新建布局在Snackbar内垂直居中显示
p.gravity= Gravity.CENTER_VERTICAL;
//将新建布局添加进snackbarLayout相应位置
snackbarLayout.addView(add_view,index,p);
}
}
效果:
详细的说明见:http://mp.weixin.qq.com/s?timestamp=1462857649&src=3&ver=1&signature=-JF5nZNnjes0PaJZzyoMNtd8AFiT2nHWVzwcdo3ZRcDs8kiukvdcmtj30KZP*9kAYhVQCV-Q0Yjq2IjvWrRpcSUbWqGgBll3jAUjMfPCPJZNgGQvSjn05*L1TsnHLqPcfSLcMZkoG9UYhfWumO7twznMfoULO3rqODcHY1N3mig=
标签:
原文地址:http://blog.csdn.net/chenmeng911/article/details/51363548