代码之家  ›  专栏  ›  技术社区  ›  James_Duh

AlertDialog样式

  •  0
  • James_Duh  · 技术社区  · 7 年前

    我们有一个项目,将运行在不同的屏幕大小的AlertDialog v7 AppComp有一个风格。

    • 我的问题是如何设置AlertDialog消息文本大小的样式?

    我已经编写了一个CustomDialog,作为一个带有自己xml文件的活动,它似乎工作得很好,只是emulator在运行时显示了一个类似幽灵的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>
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Vector    7 年前

     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();
    }
    
        2
  •  0
  •   TankRaj    7 年前

    对于您的自定义类型,您需要自己的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);
    

    要更改警报对话框主题,请在样式中定义主题。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));
    

    希望这就是你所需要的。