我正试图创建一个像Lollipop一样的警报对话框,一切都进行得很好,但我卡在了一个部分,即EditText的情况。
我想要一个带有下划线和20dp左右边距的EditText。为了实现下划线,我尝试了setBackground(),它运行良好。
但是,问题是setBackground()在API 16以下的版本中将无法运行。
为了设置边距,我尝试了
有没有办法在低于API 16的情况下为EditText设置背景并为EditText设置左右边距?
我想要一个带有下划线和20dp左右边距的EditText。为了实现下划线,我尝试了setBackground(),它运行良好。
但是,问题是setBackground()在API 16以下的版本中将无法运行。
为了设置边距,我尝试了
final EditText input = new EditText(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(30,0,30,0);
input.setLayoutParams(lp);
input.setGravity(View.TEXT_ALIGNMENT_GRAVITY);
input.setBackground(getResources().getDrawable(R.drawable.edit_text_line));
builder.setView(input);
但是需要使用完整的父元素宽度来编辑文本。
完整代码
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Message");
builder.setMessage("Do you want to\n"+""+"exit from app");
final EditText input = new EditText(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(30,0,30,0);
input.setLayoutParams(lp);
input.setGravity(View.TEXT_ALIGNMENT_GRAVITY);
input.setBackground(getResources().getDrawable(R.drawable.edit_text_line)); //call reequires api 16 and above
builder.setView(input);
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "You exit from app " + input.getText().toString(),
Toast.LENGTH_LONG).show();
}
});
AlertDialog alert = builder.create();
alert.show();
Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
nbutton.setTextColor(Color.parseColor("#7e7e7e"));
Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
pbutton.setTextColor(Color.parseColor("#109c8f"));
有没有办法在低于API 16的情况下为EditText设置背景并为EditText设置左右边距?