Java 如何在对话框功能中对齐消息并重用该功能?
我想为dialog方法创建一个函数,并在以后重用该函数 在函数中创建对话框的代码:Java 如何在对话框功能中对齐消息并重用该功能?,java,android,android-alertdialog,Java,Android,Android Alertdialog,我想为dialog方法创建一个函数,并在以后重用该函数 在函数中创建对话框的代码: private void alertView( String message ) { AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle( "Hello" ) .setIcon(R.drawable.ic_launcher) .setMessage(
private void alertView( String message ) {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle( "Hello" )
.setIcon(R.drawable.ic_launcher)
.setMessage(message)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i){
}
}).show();
}
alertView("My message");
调用此函数的代码:
private void alertView( String message ) {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle( "Hello" )
.setIcon(R.drawable.ic_launcher)
.setMessage(message)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i){
}
}).show();
}
alertView("My message");
这很好,但我想集中我的信息。我一直在寻找解决方案,并使用了各种方法,如:
AlertDialog alert = dialog.show();
TextView messageText =(TextView)alert.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
messageText.setTextColor(Color.RED)
什么都不管用。有人能帮我一下吗?从这个网站上找到了我问题的解决方案: 按照网站上的描述创建了一个xml布局,并对我的java类中的代码做了一些更改:
private void alertView( String message ) {
//create a dialog component
final Dialog dialog = new Dialog(this);
//tell the dialog to use the dialog.xml as its layout description
dialog.setContentView(R.layout.dialog);
dialog.setTitle("your title");
TextView txt = (TextView) dialog.findViewById(R.id.txt);
txt.setText(message);
Button dialogButton = (Button)dialog.findViewById(R.id.dialogButton);
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mUartCom.write("D"); //change this
}
});
dialog.show();
}
然后我通过更改消息多次重复使用此函数:
alertView("Please select one of the red icons to begin");
向下滚动到“创建自定义布局”。据我所知,
警报
将面向左侧,而简单对话框
将面向中心。请检查材料设计文件。