如何在ListPreference中改变AlertDialog的文字大小?

gemo 2014-02-19 03:53:18
我是先继承ListPreference类,重载onPrepareDialogBuilder方法,在里面改变AlertDialog,就像这样:

public class ListPreferenceEx extends ListPreference {
public ListPreferenceEx(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
super.onPrepareDialogBuilder(builder); // To change body of overridden
builder.setNegativeButton(null, null);
builder.setTitle(null);
}
}


上面代码可以成功去掉builder的按钮和标题,但是那个list的文字大小怎样变呢,因为builder没有设置theme的函数,只能在构造时指定theme,我试了如下两种方法,都没有能达到效果:

1
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {        
builder = new AlertDialog.Builder(new ContextThemeWrapper(this.getContext(), R.style.ListDlgTheme));
super.onPrepareDialogBuilder(builder);
}


2
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {        
builder = new AlertDialog.Builder(new ContextThemeWrapper(this.getContext(), R.style.ListDlgTheme));
super.onPrepareDialogBuilder(builder);
}


求指点!!!!!!!!!
...全文
305 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
哎,真难 2014-02-21
  • 打赏
  • 举报
回复
这是你的布局文件,你想怎么写就怎么写,符合你的需求就行,,,
gemo 2014-02-21
  • 打赏
  • 举报
回复
好吧,谢谢了
gemo 2014-02-20
  • 打赏
  • 举报
回复
LS啊,我还是不怎么明白,我写了个简单的demo在这里 http://good.gd/2931466.htm 点立即下载即可 就是要改变类别里面的“正常人”“脑残”“非主流”这几个字的大小,麻烦你看看好么
哎,真难 2014-02-20
  • 打赏
  • 举报
回复
http://blog.csdn.net/yunyu5120/article/details/10890921这篇帖子挺不错的,,,
哎,真难 2014-02-20
  • 打赏
  • 举报
回复
网络上有很多例子,你可以百度下,或者看下api,我一般是写个dialog.xml,然后bulider.setContentView(R.layout.dialog); 这样就可以自定义样式了,,,
gemo 2014-02-20
  • 打赏
  • 举报
回复
这个view怎么写呢,还是不太明白,我之前也用过这个方法,不过view里面都是写的自己添加的控件,而现在是要改builder的内置的item的字体,怎么在view里面写?
哎,真难 2014-02-20
  • 打赏
  • 举报
回复
额,bulider.setView(view)啊,因为只有通过view才能设置字体大小,,,
gemo 2014-02-20
  • 打赏
  • 举报
回复
在继承的ListPreference中,没有找到构造AlertDialog的时机,强行构造(就像我下面试了的两种方法),得不到正确的结果
gemo 2014-02-20
  • 打赏
  • 举报
回复
能不能看仔细点?
哎,真难 2014-02-20
  • 打赏
  • 举报
回复
AlerDialog可以加载xml,自定义一个AlerDialog就行了,,,
gemo 2014-02-20
  • 打赏
  • 举报
回复
救命呀.....
gemo 2014-02-20
  • 打赏
  • 举报
回复
R.layout.add_tab这个xml怎样写呢,LS好人做到底呀。。。。
哎,真难 2014-02-20
  • 打赏
  • 举报
回复
引用 15 楼 gemo 的回复:
那我就不读xml的数据,直接在这里硬编码设置数据啊?只要能实现效果就可以 LS帮忙再改改,能实现吗?
已经帮你改了呗,menuView = View.inflate(context1, R.layout.add_tab, null);和builder.setView(menuView);这里就是显示你想要的dialog,,,
gemo 2014-02-20
  • 打赏
  • 举报
回复
那我就不读xml的数据,直接在这里硬编码设置数据啊?只要能实现效果就可以 LS帮忙再改改,能实现吗?
哎,真难 2014-02-20
  • 打赏
  • 举报
回复
public class ListPreferenceEx extends ListPreference
{	
	Context context1;
	View menuView;
	public ListPreferenceEx(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		context1=context;
	}

	@Override
	protected void onPrepareDialogBuilder(AlertDialog.Builder builder)
	{		
		//Builder builder1=new AlertDialog.Builder(new ContextThemeWrapper(context1, R.style.AlertDialogCustom));
		//super.onPrepareDialogBuilder(builder); // To change body of overridden
												// methods use File | Settings |
												// File Templates.
		//builder=new AlertDialog.Builder(new ContextThemeWrapper(context1, R.style.AlertDialogCustom));
		menuView = View.inflate(context1, R.layout.add_tab, null);
		/*builder.setNegativeButton(null, null);
		builder.setTitle(null);*/
		builder.setView(menuView);
		
		
		
	}
}
这是我改写你的代码,显示出我想要的dialog,但是ListPreference就没有读取xml的数据,,,
gemo 2014-02-20
  • 打赏
  • 举报
回复
能不能用反射,把成员拿出来强行设置
哎,真难 2014-02-20
  • 打赏
  • 举报
回复
本来我想这样写的builder=new AlertDialog.Builder(new ContextThemeWrapper(context1, R.style.AlertDialogCustom));,但是没用读出xml的数据,,,
哎,真难 2014-02-20
  • 打赏
  • 举报
回复
帮你看了,
@Override
    protected void onPrepareDialogBuilder(Builder builder) {
        super.onPrepareDialogBuilder(builder);
        
        if (mEntries == null || mEntryValues == null) {
            throw new IllegalStateException(
                    "ListPreference requires an entries array and an entryValues array.");
        }

        mClickedDialogEntryIndex = getValueIndex();
        builder.setSingleChoiceItems(mEntries, mClickedDialogEntryIndex, 
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        mClickedDialogEntryIndex = which;

                        /*
                         * Clicking on an item simulates the positive button
                         * click, and dismisses the dialog.
                         */
                        ListPreference.this.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                        dialog.dismiss();
                    }
        });
        
这是onPrepareDialogBuilder的源码,因为你在方法里面写super.onPrepareDialogBuilder(builder);所以它直接继承了父类的方法,所以改变不了,但是如果你要把这句话删除,就可以自己写dialog了,但是ListPreference的数据有读不出来了,你自己可以试试,,,
哎,真难 2014-02-20
  • 打赏
  • 举报
回复
刚在开会,一会帮你看,,,

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧