彈框(AlertDialog)和提示信息Toast字體大小顏色設置


一、AlertDialog:

             AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("溫馨提示");
builder.setMessage("是否進行下一個病人?");
builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//處理你的操作

}
});
builder.setNegativeButton("否", null);
AlertDialog dialog = builder.create();
dialog.show();
//彈框設置字體顏色
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.BLUE);
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(Color.BLUE);
try {
//獲取mAlert對象
Field mAlert = AlertDialog.class.getDeclaredField("mAlert");
mAlert.setAccessible(true);
Object mAlertController = mAlert.get(dialog);
//獲取mMessageView並設置大小顏色
Field mMessage = mAlertController.getClass().getDeclaredField("mMessageView");
mMessage.setAccessible(true);
TextView mMessageView = (TextView) mMessage.get(mAlertController);
mMessageView.setTextColor(Color.BLUE);
// mMessageView.setTextSize(30);
//獲取mTitleView並設置大小顏色
Field mTitle = mAlertController.getClass().getDeclaredField("mTitleView");
mTitle.setAccessible(true);
TextView mTitleView = (TextView) mTitle.get(mAlertController);
mTitleView.setTextColor(Color.BLUE);
// mTitleView.setTextSize(30);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}

二、Toast:

private Toast mToast;
private void showTip(final String str){
runOnUiThread(new Runnable() {
@Override
public void run() {
if (mToast == null) {
mToast = Toast.makeText(getApplicationContext(), "",
Toast.LENGTH_SHORT);
LinearLayout layout = (LinearLayout) mToast.getView();
TextView tv = (TextView) layout.getChildAt(0);
tv.setTextSize(18);
          tv.setTextColor(R.color.white);

}
//mToast.cancel();
mToast.setGravity(Gravity.CENTER, 0, 0);
mToast.setText(str);
mToast.show();
}
});
}

 

  例如:Toast提示"登錄成功",直接使用showTip("登錄成功")即可。
注意:fragment里面使用時將runOnUiThread替換成getActivity().runOnUiThread即可。
 
        
 
        

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM