【发布时间】:2019-01-06 21:11:54
【问题描述】:
我们有一个项目可以在不同的屏幕尺寸上运行,AlertDialog v7 AppComp 有一个样式。
- 我的问题是如何设置 AlertDialog 消息文本大小的样式?
- 第二个问题如何改变AlertDialog的大小以适应不同的屏幕尺寸?
我用它自己的 xml 文件编写了一个 CustomDialog 作为一个 Activity,它似乎工作正常,除了模拟器在运行时显示 xml 文件的幽灵般的视图!我最近看到一篇帖子暗示无法更改消息的文本大小。我对如何使用 DisplayMetrics 有一些了解,但我不想使用这个约定。
下面的 AletDialog 和样式的设计代码。如果有人可以向我保证幽灵图像不会出现在真实设备上,我可能会放弃并使用这种看起来很笨重的方法
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>
【问题讨论】:
-
为您的警报对话框创建自定义布局,这将使您可以访问 textview 的样式、点击等,然后将您的对话框视图设置为该布局..
-
@AalapPatel 所以只创建一个自定义 xml 文件,而不使用 ActivityCustom 并在使用 CustomDialog 的 Activity 中扩展自定义 xml
-
什么是没有 ActivityCustom??
-
@AalapPatel 我开始创建 Activity 和相应的 xml 文件,所以这是第一个错误。根据 Grendel 的回答,我只需要 xml 文件和一种捕获视图的方法作为 Nob,我可以犯各种错误
标签: android android-alertdialog