AlertDialog.Builder builder = newAlertDialog.Builder(new ContextThemeWrapper(this,R.style.AlertDialogCustom)); 然後自定義自己的樣式就可以了
-
<?xml version="1.0" encoding="utf-8"?>
-
<resources>
-
<style name="AlertDialogCustom" parent="@android:style/AlertDialog">
-
<item name="android:textColor">#00FF00</item>
-
<item name="android:typeface">monospace</item>
-
<item name="android:textSize">10sp</item>
-
</style>
-
</resources>
複製代碼
1. 定義佈局文件:alertdialog_item.xml,因爲列表的每一項的樣式都是從佈局文件設置的,注意的是佈局文件是一個TextView
-
<?xml version="1.0" encoding="utf-8"?>
-
<TextViewxmlns:android="http://schemas.android.com/apk/res/android"
-
android:id="@+id/alertdialog_item"
-
android:layout_width="fill_parent"
-
android:layout_height="30dp"
-
android:layout_gravity="center_horizontal"
-
android:gravity="center_horizontal"
-
android:textColor="#000000"
-
android:background="#668B8B"
-
android:textSize="28px" >
-
</TextView>
複製代碼
2. 設置AlertDialog的總體樣式,比如說割線的高度,這時候在style.xml配置
<!-- 自定義AlertDialog的樣式 -->
-
<stylename="AlertDialogCustom">
-
<item name="android:dividerHeight">5dp</item>
-
<itemname="android:width">100dp</item>
-
</style>
複製代碼
3. 在Activity上設置相應的AlertDialog
-
String[] menu_names =new String[]{"進入","取消","刪除","刪除全部"};
-
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this,R.style.AlertDialogCustom));
-
builder.setAdapter(newArrayAdapter(WeiXunListActivity.this, R.layout.alertdialog_item,menu_names),null);
-
builder.create().show();
複製代碼
設置文字的字體和大小
在AndroidSDK中使用Typeface類來定義字體,可以通過常用字體類型名稱進行設置,如設置默認黑體:
-
Paint mp = new paint();
-
mp.setTypeface(Typeface.DEFAULT_BOLD)
複製代碼
常用的字體類型名稱還有:
* Typeface.DEFAULT //常規字體類型
* Typeface.DEFAULT_BOLD //黑體字體類型
* Typeface.MONOSPACE //等寬字體類型
* Typeface.SANS_SERIF //sans serif字體類型
* Typeface.SERIF //serif字體類型
除了字體類型設置之外,還可以爲字體類型設置字體風格,如設置粗體:
-
Paint mp = new Paint();
-
Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
-
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)來實現,儘管效果看起來不是很實在(字體中空效果)。實際發現,最後繪製的效果與手機硬件也有些關係,比如前面的繪圖測試程序.