2017-09-05 52 views
0

我们有一个项目,可以在不同的屏幕尺寸上运行AlertDialog v7 AppComp的风格。我的问题是如何设置AlertDialog消息文本大小的样式?第二个问题,如何更改各种屏幕尺寸的AlertDialog大小?我用一个自己的xml文件写了一个CustomDialog作为一个Activity,这似乎工作正常,除了模拟器在运行时显示一个像xml文件视图的鬼魂!我最近看到过一篇文章暗示邮件的文本大小不能改变。我有一些关于如何使用DisplayMetrics的知识,但不想使用这个约定。为以下AletDialog和样式设计代码。如果有人能保证我的鬼影将不是一个真正的设备上显示出来,我可能干脆放弃,并用这个方法似乎笨重AlertDialog样式

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> 
+0

为您的警告对话框创建自定义布局,这会给你所有访问的TextView的造型,点击次数和等,然后设置对话框中查看到那个布局.. –

+0

@AalapPatel所以只需创建一个自定义xml文件,而无需使用ActivityCustom并在使用CustomDialog的Activity的内部充入自定义xml –

+0

什么是没有ActivityCustom? –

回答

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

这一行代码为什么我使用的openDialog前缀有点困惑Button btnYES =(Button)findViewById(R.id.btnYES );它失败了也许有人可以详细说明这一点,但YES很好,简单,很简单 –

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> 

和s以主题对话为:

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

希望这是你需要的一切。

+0

@Krantz因此,我创建一个custom.xml文件,然后在使用该对话框的Activity中充气该活动我使用LayoutParams来设置不同设备的大小还有一点关于“你自己的布局” –

+0

我已经在我的样式文件中使用了 10sp,并且它不会更改文本对话框消息文本的大小,所以我想我需要知道如何调用一个活动中的自定义xml文件 –