2016年7月7日 星期四

[Android]修改AlertDialog預設的字型

先來看一下效果



程式碼如下


package come.testdialog;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    //font path
    private final String FONT_BOLD = "fonts/RobotoCondensed-Bold.ttf";
    private final String FONT_REGULAR = "fonts/RobotoCondensed-Regular.ttf";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //call show dialog function
        showAlertDialog(MainActivity.this, "title", "Demo on Boywhy Blog", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this,"click setPositiveButton",Toast.LENGTH_LONG).show();
            }
        },"Yes","No");
    }

    /***
     *
     * @param context Context
     * @param title  Title String
     * @param message Message String
     * @param negativeEvent Negative Button Event
     * @param positiveEvent Positive Button Event
     * @param positiveStr   Positive Button String
     * @param negativeStr    Negative Button String
     * @return  AlertDialog Object
     */
    public AlertDialog showAlertDialog(final Context context, String title, String message, DialogInterface.OnClickListener negativeEvent, DialogInterface.OnClickListener positiveEvent, String positiveStr, String negativeStr) {

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        //set icon
        builder.setIcon(R.drawable.operation_icon_warning);
        //set message string
        builder.setMessage(message);
        //set title string
        builder.setTitle(title);
        //set positiveButton String and Click Event
        builder.setPositiveButton(positiveStr, positiveEvent);
        //set NegativeButton String and Click Event
        builder.setNegativeButton(negativeStr, negativeEvent);
        AlertDialog alertDialog = builder.create();
        //show dialog
        alertDialog.show();
        //get Title TextView Object
        TextView textView = ((TextView) alertDialog.findViewById(context.getResources().getIdentifier(
                "alertTitle", "id", "android")));
        //set Title TextView TextColor to red
        textView.setTextColor(context.getResources().getColor(android.R.color.holo_red_light));
        //change TextView font typeface to FONT_BOLD
        setTextFontFamily(textView, true);
        //get Message TextView Object
        TextView msgView = ((TextView) alertDialog.findViewById(context.getResources().getIdentifier(
                "message", "id", "android")));
        //set Message TextColor to blue
        msgView.setTextColor(context.getResources().getColor(android.R.color.holo_blue_bright));


        //get Positive Button Object
        Button positive = ((Button) alertDialog.getButton(AlertDialog.BUTTON_POSITIVE));
        //Set Positive TextColor to blue
        positive.setTextColor(context.getResources().getColor(android.R.color.holo_blue_bright));
        //change TextView font typeface to FONT_REGULAR
        setTextFontFamily(positive, false);
        //set Positive Button Text Size 20
        positive.setTextSize(20);
        //positive.setText(positiveStr);

        //get negative Button Object
        Button negative = ((Button) alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE));
        //set negative Button Text Color to white
        negative.setTextColor(context.getResources().getColor(android.R.color.white));
        //change TextView font typeface to FONT_REGULAR
        setTextFontFamily(negative, false);

        return alertDialog;
    }

    /**
     * set TextView Font
     *
     * @param v    TextView
     * @param bold true -> RobotoCondensed-Bold , false -> RobotoCondensed-Regular
     */
    public void setTextFontFamily(TextView v, boolean bold) {
        if (bold)
            v.setTypeface(Typeface.createFromAsset(getAssets(), FONT_BOLD));
        else
            v.setTypeface(Typeface.createFromAsset(getAssets(), FONT_REGULAR));
    }

    /**
     * set TextView Font
     *
     * @param v    Button
     * @param bold true -> RobotoCondensed-Bold , false -> RobotoCondensed-Regular
     */
    public void setTextFontFamily(Button v, boolean bold) {
        if (bold)
            v.setTypeface(Typeface.createFromAsset(getAssets(), FONT_BOLD));
        else
            v.setTypeface(Typeface.createFromAsset(getAssets(), FONT_REGULAR));
    }

}

記得要新增一個assets資料夾,並且建一個font子資料夾,並且把字型放進去。

沒有留言:

張貼留言