AlertDialog样式

时间:2017-09-05 17:29:44

标签: android alertdialog

我们有一个项目将在不同的屏幕尺寸上运行,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>

2 个答案:

答案 0 :(得分:1)

@James_Duh经过一番广泛的测试后,我删除了我之前的答案。膨胀activity_custom.xml文件有很多问题。所以更好的想法是使用setContentView。您仍然需要为将为代码开发的所有设备屏幕创建activity_custom.xml

像任何其他变量一样声明这个

 private Context context = this;

然后,这是打开并显示activity_custom.xml文件的方法,并显示您在各种设备上测试的新的和改进的Dialog,它运行良好

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

答案 1 :(得分:0)

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

并将主题设置为对话框:

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

希望这就是你所需要的一切。