如何以编程方式在Android的快餐栏中添加edittext?
问题描述:
我的申请中有Snackbar
.我想在Snackbar
中添加Edittext
以接受一些输入.如何在Snackbar
中添加Edittext
?
I have Snackbar
in my application. I want to add Edittext
in Snackbar
to accept some input. How can I add an Edittext
in Snackbar
?
答
//Custom layouts are discouraged due to the intended use of Snackbars,but this will do your task!
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linear_layout_root);
final Snackbar snackbar = Snackbar.make(linearLayout, "Hey Whats Up", Snackbar.LENGTH_INDEFINITE);
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
// Inflate your custom view with an Edit Text
LayoutInflater objLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View snackView = objLayoutInflater.inflate(R.layout.custom_snac_layout, null); // custom_snac_layout is your custom xml
layout.addView(snackView, 0);
snackbar.show();