Android AlertDialog样式
我们有一个项目,将运行在不同的屏幕大小的AlertDialog v7 AppComp有一个风格 我的问题是如何设置AlertDialog消息文本大小的样式? 第二个问题如何更改不同屏幕大小的AlertDialog的大小? 我已经编写了一个CustomDialog,作为一个活动,它有自己的xml文件,除了emulator在运行时显示xml文件的幻影视图外,它似乎工作得很好!我最近看到一篇帖子,暗示消息的文本大小不能更改。我有一些关于如何使用DisplayMetrics的知识,但不想使用这个约定 下面是AletDialog和样式的设计代码。如果有人能向我保证幽灵图像不会出现在真正的设备上,我可能会放弃使用这种方法,因为它看起来很笨重Android AlertDialog样式,android,android-alertdialog,Android,Android Alertdialog,我们有一个项目,将运行在不同的屏幕大小的AlertDialog v7 AppComp有一个风格 我的问题是如何设置AlertDialog消息文本大小的样式? 第二个问题如何更改不同屏幕大小的AlertDialog的大小? 我已经编写了一个CustomDialog,作为一个活动,它有自己的xml文件,除了emulator在运行时显示xml文件的幻影视图外,它似乎工作得很好!我最近看到一篇帖子,暗示消息的文本大小不能更改。我有一些关于如何使用DisplayMetrics的知识,但不想使用这个约定 下
private void doWhat() {
// R.style.MyAlertDialogStyle see res/values/styles
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle);
// Setting Dialog Title
alertDialog.setTitle("Confirm Reset of Password");
// Setting Dialog Message
alertDialog.setMessage("Click YES to create a new master password");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.caution);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke YES event
db = helper.getReadableDatabase();
String q = "SELECT * FROM masterPW";
Cursor cursor = db.rawQuery(q,null);
// Above query gets TABLE_PW data from Col_IDI
// TABLE_PW will only ever have one row of data
int rowID = 99;
if(cursor.moveToFirst()){
rowID = cursor.getInt(cursor.getColumnIndex(Col_IDI));
str = cursor.getString(cursor.getColumnIndex(Col_MPW));
}
cursor.close();
// Line of code below WORKS deletes entire TABLE <=====
// Not a recomended way to re-set the master password
// db.delete(TABLE_PW, null, null);
String num = Integer.toString(rowID);
db.delete(TABLE_PW, Col_IDI + " = ?", new String[] { num });
db.close();
Intent intentYY = new Intent(DetailsActivity.this, MainActivity.class );
startActivity( intentYY );
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "Password NOT Changed", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
// Showing Alert Message and set the SIZE of the alertDialog
alertDialog.show().getWindow().setLayout(1300, 500);// was 1100 500
}
<!--Code below styles the AlertDialog.Builder on DetailsActivity -->
<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Dialog.Alert">
<!-- Used for the buttons -->
<item name="colorAccent">@color/color_deepBlue</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">@color/color_Black</item>
<item name="android:textSize">25sp</item>
<!-- Used for the background -->
<item name="android:background">@color/color_lightGray</item>
</style>
对于您的自定义类型,您需要自己的AlertDialog布局,而不是默认布局,要更改AlertDialog的大小,您可以使用:
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = 150;
lp.height = 500;
lp.x=-170;
lp.y=100;
alertDialog.getWindow().setAttributes(lp);
要更改警报对话框主题,请将style.xml中的主题定义为:
<resources>
<style name="AlertDialogTheme" parent="@android:style/Theme.Dialog">
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">10sp</item>
</style>
</resources>
希望这就是你所需要的。@James_Duh经过大量测试后,我删除了之前的答案。膨胀activity_custom.xml文件有许多问题。因此,更好的方法是使用setContentView。您仍然需要为所有设备屏幕创建activity_custom.xml,您将为下面的代码开发这些屏幕 像任何其他变量一样声明它
private Context context = this;
然后,这里是打开和显示activity_custom.xml文件的方法,并显示我在各种设备上测试的新的和改进的对话框,效果非常好
public void doWhat(){
final Dialog openDialog = new Dialog(context);
openDialog.setContentView(R.layout.activity_custom);
Button btnYES = (Button)openDialog.findViewById(R.id.btnYES);
// if YES delete Master Password from TABLE_MPW
btnYES.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openDialog.dismiss();
Intent intent = new Intent( DetailsActivity.this, ListActivity.class );
startActivity( intent );
Toast.makeText(getApplicationContext(), "Password WAS Changed", Toast.LENGTH_SHORT).show();
}
});
openDialog.show();
}
为警报对话框创建自定义布局,这将使您能够访问textview的样式、单击等,然后将对话框视图设置为该布局。@AalapPatel因此只需创建一个没有ActivityCustom的自定义xml文件,并在使用CustomDialogwhat is with the ActivityCustom custom的活动中膨胀自定义xml???@AalapPatel我开始创建和活动以及相应的xml文件,这是第一个错误。根据Grendel的回答,我只需要xml文件和一种将视图捕获为Nob的方法,我可以对其进行各种处理mistakes@Krantz因此,我创建了一个custom.xml文件,并在使用对话框的活动中对其进行膨胀,然后在该活动中,我使用LayoutParams来设置不同设备的大小,这与您自己的layoutI使用10sp,它不会改变对话框消息文本的文本大小,因此我想我需要知道如何在活动中调用自定义xml文件。这行代码让我有点困惑,为什么openDialog前缀我使用Button btnYES=ButtonfindViewByIdR.id.btnYES;它失败了,也许有人可以详细说明这一点,但是的,作品伟大而简单,因为简单是好的
public void doWhat(){
final Dialog openDialog = new Dialog(context);
openDialog.setContentView(R.layout.activity_custom);
Button btnYES = (Button)openDialog.findViewById(R.id.btnYES);
// if YES delete Master Password from TABLE_MPW
btnYES.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openDialog.dismiss();
Intent intent = new Intent( DetailsActivity.this, ListActivity.class );
startActivity( intent );
Toast.makeText(getApplicationContext(), "Password WAS Changed", Toast.LENGTH_SHORT).show();
}
});
openDialog.show();
}