我们如何使用样式自定义警报对话框的字体?

时间:2018-10-10 07:12:16

标签: android material-design android-alertdialog android-styles

我们如何使用样式自定义android警报对话框的字体

  

我发现了许多使用setTypeFace()方法的解决方案。但是我想使用样式来自定义整个应用程序警报对话框。

我想更改标题,消息,按钮的字体。

我可以使用以下代码更改消息字体。

我的警报对话框样式声明

<style name="MyAlertDialougeTheme" parent="@android:style/Theme.Material.Light.Dialog.Alert">
    <item name="android:textAppearanceSmall">@style/MyTextAppearance</item>
    <item name="android:textAppearanceLarge">@style/MyTextAppearance</item>
    <item name="android:textAppearanceMedium">@style/MyTextAppearance</item>

</style>

用于显示警报对话框的Java代码

 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this,
                R.style.MyAlertDialougeTheme);
        builder.setTitle("Warning")
                .setMessage("My message here")
                .setPositiveButton("yes", null)
                .setNegativeButton("no", null)
                .show();

在屏幕下方查看

enter image description here

请帮助我使用样式更改标题和按钮的字体,并且我想自定义否定和肯定按钮的字体颜色。

提前感谢您的时间和帮助!

3 个答案:

答案 0 :(得分:1)

首先创建一个CustomDialog类,它将扩展Android的Dialog类。以下是相同的代码-

public class CustomDialog extends Dialog implements
        View.OnClickListener {

    Activity context;
    private Button mBtnOK;

    public CustomDialog(Activity context) {
        super(context);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.custom_popup_dialog_box);
        mBtnOK = findViewById(R.id.btn_ok);
        mBtnOK.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_ok:
                dismiss();
                break;
            default:
                break;
        }
        dismiss();
    }
}

现在在这里,只要您想要Dialog的实例,就只需要创建CustomDialog类的实例即可,并且 custom_popup_dialog_box 处的xml将具有所有自定义字体,例如Font系列,文字大小,颜色等。您只需要在xml内或以编程方式设置属性。希望您找到解决方案。 如果需要进一步的信息,请告诉我。谢谢。

答案 1 :(得分:0)

用于显示警报对话框代码的Java代码替换此代码

<!--content-->
<div id="content">
  <div class="clear">

 <br />
<div id="container" class="wrapper-container">
<div class="corner-top-left">
<div class="corner-top-right">
<div class="corner-bottom-left">
<div class="corner-bottom-right">

   <div class="clear" style="height:325px;overflow-y: scroll;">

<table class="contentpaneopen">
<div class="left" id="left" >

     <?php
                 $selectSQL = "SELECT stories FROM `stories` where 
 `subject`='{$_GET['sub']}'";
   # Execute the SELECT Query
  if( !( $selectRes = mysql_query( $selectSQL ) ) ){
echo 'Retrieval of data from Database Failed - #'.mysql_errno().': 
 '.mysql_error();
    }else{
   ?>
    <table border="2" align="center">

布局保存lay_alertdialog

  if( mysql_num_rows( $selectRes )==0 ){
    echo '<tr><td colspan="4">No Rows Returned</td></tr>';
  }else{
    while( $row = mysql_fetch_assoc( $selectRes ) ){
     {$row['date/time']}</td></tr>\n";
     echo '<tr>';

                       echo '<td class="leftpart">';

if (isset($_GET['sub'])) {
echo '<h2>Subject:  ' . $_GET['sub'] . '</h2>'; 
} else {
// Fallback behaviour goes here
}


                        echo '<h2>Description:  ' . $row['stories'] . '</h2>'; 
                        //echo '<h3>' . $row['stories'] . '<h3>';

                    echo '</td>';

   echo '</tr>';

    echo '<tr>';
 echo '<h2>Name: '  . $_SESSION['name'] . '</h2>'; 
  $selectSQL = "SELECT address,country,contact,email FROM `user` where 
  `name`='{$_SESSION['name']}'";
     if( !( $selectRes = mysql_query( $selectSQL ) ) ){
     echo 'Retrieval of data from Database Failed - #'.mysql_errno().': 
     '.mysql_error();
      }else{
    if( mysql_num_rows( $selectRes )==0 ){
     echo '<tr><td colspan="4">No Rows Returned</td></tr>';
     }else{
      while( $row = mysql_fetch_assoc( $selectRes ) ){
        echo '<h2>Address:    '  . $row['address'] . '</h2>'; 
        echo '<h2>Country:    '  . $row['country'] . '</h2>'; 
        echo '<h2>Phone No:    '  . $row['contact'] . '</h2>'; 
         echo '<h2>Email:    '  . $row['email'] . '</h2>'; 
            }
        }
         }
         echo '</tr>';



  }
  }

 }


    ?>
  </tbody>
     </table>
        <!--</form>--></div></table></div>
        <div id="editor"></div>
         <button id="cmd">generate PDF</button>
           <div class="clear">


    </div></div></div></div></div></div></div>

   <!--footer-->
    <div class="footer clear">
       <span>Crime Reporting System &copy; 2016  |  <a href="#">Privacy 
  Policy</a></span> Crime Reporting is for registering FIR any time and any 
   where in India.. &nbsp;&nbsp;&nbsp;<!--{%FOOTER_LINK} -->
    </div>
</div>

  <script src="js/jquery.validate.min.js"></script>
    <script src="js/login.js"></script>
 </body></html>

答案 2 :(得分:0)

您正在寻找的是: https://stackoverflow.com/a/10741161/10126669

您需要做的就是将字体放入资产中并进行更改

SpannableStringBuilder SS = new SpannableStringBuilder("My message here");
SS.setSpan (new CustomTypefaceSpan("", font2), 0, 4,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);

以匹配您的SpannableStringBuilder的大小

SS.setSpan (new CustomTypefaceSpan("", font2), 0, SS.length(),Spanned.SPAN_EXCLUSIVE_INCLUSIVE);

,然后将SpannableStringBuilder添加为对话框

.setMessage(SS)