AlertDialog自定義樣式

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

然後自定義自己的樣式就可以了
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3.     <style name="AlertDialogCustom" parent="@android:style/AlertDialog"> 
  4.         <item name="android:textColor">#00FF00</item> 
  5.         <item name="android:typeface">monospace</item> 
  6.         <item name="android:textSize">10sp</item> 
  7.     </style> 
  8. </resources>   
複製代碼


1.      定義佈局文件:alertdialog_item.xml,因爲列表的每一項的樣式都是從佈局文件設置的,注意的是佈局文件是一個TextView

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TextViewxmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:id="@+id/alertdialog_item"
  4.     android:layout_width="fill_parent"
  5.    android:layout_height="30dp"
  6.     android:layout_gravity="center_horizontal"
  7.     android:gravity="center_horizontal"
  8.     android:textColor="#000000"
  9.     android:background="#668B8B"
  10.     android:textSize="28px" >
  11. </TextView>
複製代碼

2.      設置AlertDialog的總體樣式,比如說割線的高度,這時候在style.xml配置

<!-- 自定義AlertDialog的樣式 -->

  

  1.   <stylename="AlertDialogCustom">
  2.        <item name="android:dividerHeight">5dp</item>
  3.         <itemname="android:width">100dp</item>
  4.     </style>
複製代碼

3.      在Activity上設置相應的AlertDialog

   

  1. String[] menu_names =new String[]{"進入","取消","刪除","刪除全部"};
  2.     AlertDialog.Builder builder  = new AlertDialog.Builder(new ContextThemeWrapper(this,R.style.AlertDialogCustom));
  3.     builder.setAdapter(newArrayAdapter(WeiXunListActivity.this, R.layout.alertdialog_item,menu_names),null);
  4.     builder.create().show();
複製代碼

設置文字的字體和大小

在AndroidSDK中使用Typeface類來定義字體,可以通過常用字體類型名稱進行設置,如設置默認黑體:

  1. Paint mp = new paint();
  2. mp.setTypeface(Typeface.DEFAULT_BOLD)
複製代碼


常用的字體類型名稱還有:

  * Typeface.DEFAULT //常規字體類型

  * Typeface.DEFAULT_BOLD //黑體字體類型

  * Typeface.MONOSPACE //等寬字體類型

  * Typeface.SANS_SERIF //sans serif字體類型

  * Typeface.SERIF //serif字體類型

除了字體類型設置之外,還可以爲字體類型設置字體風格,如設置粗體:

  1. Paint mp = new Paint();
  2. Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
  3. p.setTypeface( font );
複製代碼


常用的字體風格名稱還有:

  * Typeface.BOLD //粗體

  * Typeface.BOLD_ITALIC //粗斜體

  * Typeface.ITALIC //斜體

  * Typeface.NORMAL //常規


但是有時上面那些設置在繪圖過程中是不起作用的,所以還有如下設置方式:

Paint mp = new Paint();
mp.setFakeBoldText(true); //true爲粗體,false爲非粗體
mp.setTextSkewX(-0.5f); //float類型參數,負數表示右斜,整數左斜
mp.setUnderlineText(true); //true爲下劃線,false爲非下劃線
mp.setStrikeThruText(true); //true爲刪除線,false爲非刪除線


Paint
常用的方法還有:

mp.setTextSize(); //設置字體大小,int型,如12
mp.setStrokeWidth(w); //設置線寬,float型,如2.5f,默認繪文本無需設置(默認值好像爲0),但假如設置了,再繪製文本的時候一定要恢復到0


個人總結說明:對於中文粗體的設置,好像只能通過setFakeBoldText(true)來實現,儘管效果看起來不是很實在(字體中空效果)。實際發現,最後繪製的效果與手機硬件也有些關係,比如前面的繪圖測試程序.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章