【问题标题】:AlertDialog styling警报对话框样式
【发布时间】: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


【解决方案1】:

@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();
}

【讨论】:

  • 对这行代码有点困惑为什么我使用 openDialog 前缀 Button btnYES = (Button)findViewById(R.id.btnYES);它失败了也许有人可以详细说明这一点,但是 YES 工作得很好而且很简单,因为简单就是很好
【解决方案2】:

对于您的自定义类型,您需要自己的警报对话框布局(不是默认布局)并更改您可以使用的警报对话框的大小:

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>

并将主题设置为对话框:

 AlertDialog.Builder builder = new AlertDialog.Builder(new 
ContextThemeWrapper(this, R.style.AlertDialogTheme));

希望这就是你所需要的。

【讨论】:

  • @Krantz 所以我创建了一个 custom.xml 文件并在使用对话框的 Activity 中对其进行膨胀,然后在该 Activity 中我使用 LayoutParams 为不同设备设置大小仍然有点关于“你自己的布局”
  • 我在我的样式文件中使用了 10sp 并且它不会改变对话框消息文本的文本大小,所以我想我需要知道如何在 Activity 中调用自定义 xml 文件
猜你喜欢
  • 2011-02-01
  • 2020-03-06
  • 2014-12-14
  • 1970-01-01
  • 2020-12-30
  • 2016-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多