2017-09-05 26 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文件 –