如何通过反射来解决AlertDialog标题由于字数过多显示不全的问题
生活随笔
收集整理的這篇文章主要介紹了
如何通过反射来解决AlertDialog标题由于字数过多显示不全的问题
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
轉(zhuǎn)載前請標明出處:http://blog.csdn.net/sahadev_
先上一下示例圖:
這是默認狀態(tài)下:這是通過反射后修改的結(jié)果:
在解決這個問題之前首先需要了解一下AlertDialog的基本構(gòu)造,所以先從源碼看起:
想要知道為什么顯示不全,首先入口處應(yīng)該是這里:
builder.setTitle("關(guān)于印發(fā)《省環(huán)境監(jiān)察局關(guān)于開展黨的群眾路線教育實踐活動的實施方案》的通知");然后進入setTitle的方法:
/*** Set the title displayed in the {@link Dialog}.** @return This Builder object to allow for chaining of calls to set methods*/public Builder setTitle(CharSequence title) {P.mTitle = title;return this;}
好了,它把字符串賦給了對象P,然后再來看看P的類型:
public static class Builder {private final AlertController.AlertParams P;private int mTheme;/*** Constructor using a context for this builder and the {@link AlertDialog} it creates.*/public Builder(Context context) {this(context, resolveDialogTheme(context, 0));}
嗯,從Builder處可以看到P是類型為AlertController.AlertParams的對象。然后再接著看AlertController.AlertParams這個類里面的屬性( 注意:如果你沒有專門設(shè)置過可以查看Android內(nèi)部類的方法的話,這里是看不了的,相關(guān)設(shè)置可以參見:http://www.2cto.com/kf/201311/259006.html):
好了,進入AlertController.AlertParams類內(nèi)可以看到該類是屬于AlertController的內(nèi)部類,以下為該類的部分屬性:
public static class AlertParams {public final Context mContext;public final LayoutInflater mInflater;public int mIconId = 0;public Drawable mIcon;public int mIconAttrId = 0;public CharSequence mTitle;
好了,所以那個字符串設(shè)置時最終會設(shè)置到這個類對象的mTitle處,然后接下來就是要查看這個屬性什么時候被使用了呢:
在內(nèi)類可以看到該方法使用了該屬性:
public void apply(AlertController dialog) {if (mCustomTitleView != null) {dialog.setCustomTitle(mCustomTitleView);} else {if (mTitle != null) {dialog.setTitle(mTitle);}
這里說明mTitle被設(shè)置給了AlertController的對象dialog,然后接下來就是尋找這個dialog對象是怎么被傳入進來的:
通過尋找找到apply該方法的被調(diào)用處是:
/*** Creates a {@link AlertDialog} with the arguments supplied to this builder. It does not* {@link Dialog#show()} the dialog. This allows the user to do any extra processing* before displaying the dialog. Use {@link #show()} if you don't have any other processing* to do and want this to be created and displayed.*/public AlertDialog create() {final AlertDialog dialog = new AlertDialog(P.mContext, mTheme, false);P.apply(dialog.mAlert);dialog.setCancelable(P.mCancelable);if (P.mCancelable) {dialog.setCanceledOnTouchOutside(true);}dialog.setOnCancelListener(P.mOnCancelListener);dialog.setOnDismissListener(P.mOnDismissListener);if (P.mOnKeyListener != null) {dialog.setOnKeyListener(P.mOnKeyListener);}return dialog;}
該方法位于AlertDialog.Builder的內(nèi)部,也就是當Builder對象調(diào)用create方法時會將 AlertController的對象dialog傳入,這里可以看到是dialog的mAlert屬性,也就是說dialog的 mAlert屬性是 AlertController的對象。好了,到這里分析完畢?,F(xiàn)在就剩下取出該對象,對該對象進行反射了。
AlertDialog dialog = builder.create();try {Class<?> mAlert = dialog.getClass();Log.e("sahadev", mAlert.getName());Field field = mAlert.getDeclaredField("mAlert");field.setAccessible(true);Log.e("sahadev", field.getName() + "----" + field.get(dialog));Field mTitleView = field.get(dialog).getClass().getDeclaredField("mTitleView");mTitleView.setAccessible(true);Object AlertController = field.get(dialog);mTitleView.set(AlertController, new TextView(this));//該方法<span style="font-family:Microsoft YaHei;">沒起作用,不知道為什么,有大神清楚么?</span>dialog.show();Object obj = mTitleView.get(AlertController);TextView textView = (TextView) obj;textView.setSingleLine(false);} catch (Exception e) {e.printStackTrace();} 好了,到了這里就解決完畢了。
如有問題請留言。
總結(jié)
以上是生活随笔為你收集整理的如何通过反射来解决AlertDialog标题由于字数过多显示不全的问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Tensorflow实现MLP
- 下一篇: 唐刘之辩:行业知识图谱的schema构建