您的位置:首页 > Web前端 > CSS

AlertDialog自定义样式

2014-10-25 16:14 323 查看
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)来实现,尽管效果看起来不是很实在(字体中空效果)。实际发现,最后绘制的效果与手机硬件也有些关系,比如前面的绘图测试程序.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android ui alertdialog