您的位置:首页 > 移动开发

Dialog(八)——改变系统自带Dialog字体大小(ContextThemeWrapper)

2014-06-06 15:44 393 查看
MainActivity如下:

package c.c.testdialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
 * Demo描述:
 * 改变系统对话框中的字体大小
 * 方法:
 * 0 将一个style的parent设置为@android:style/Theme.Dialog
 *   修改其中的 <item name="android:textSize">30sp</item>
 *   这种方式很类似于子类覆盖父类方法
 * 1 利用context和该style生成ContextThemeWrapper
 * 2 利用ContextThemeWrapper生产Builder对象
 */
public class MainActivity extends Activity {
   private Button mButton;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}
	private void init(){
		mButton=(Button) findViewById(R.id.button);
		mButton.setOnClickListener(new ClickListenerImpl());
	}
	private class ClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.button:
				// 弹出自定义对话框
				showDialog();
				break;
			default:
				break;
			}

		}
	}
	private void showDialog(){
		Dialog dialog = null;
		ContextThemeWrapper contextThemeWrapper = 
		new ContextThemeWrapper(MainActivity.this, R.style.dialog);
		Builder builder =  new AlertDialog.Builder(contextThemeWrapper);
		builder.setItems(R.array.share_array,
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						switch (which) {
						case 0:
							System.out.println("----->发送邮件");
							break;
						case 1:
							System.out.println("----->分享到FaceBook");
							break;
						case 2:
							System.out.println("----->分享到Twitter");
							break;
						default:
							break;
						}
					}
				});
		dialog = builder.create();
		dialog.show();
	}
}


arrays.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="share_array">
        <item>发送邮件</item>
        <item>分享到FaceBook</item>
        <item>分享到Twitter</item>
    </string-array>
</resources>


styles.xml如下:

<resources>

    <style name="AppBaseTheme" parent="android:Theme.Light"></style>

    <style name="AppTheme" parent="AppBaseTheme"></style>

    <style name="dialog" parent="@android:style/Theme.Dialog">
        <item name="android:textSize">30sp</item>
    </style>

</resources>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: