最近一直在学习dhroid框架,从配置框架到学习里面的东西,遇到了不少的问题。现在一 一记录下自己遇到的问题。
首先,工程中框架配置好后,是有dialog没有效果。
Application类中进行全局配置:
public class MyApplication extends Application{
/**
* dhroid框架在application中的配置
*/
@Override
public void onCreate() {
super.onCreate();
IocContainer.getShare().initApplication(this);
IocContainer.getShare().bind(MyDialogImpl.class).to(IDialog.class) //这里使用的是默认配置
//这是单例
.scope(InstanceScope.SCOPE_SINGLETON);
//配置ValueFix对象基本每个项目都有自己的实现
IocContainer.getShare().bind(MyValueFixer.class)
.to(ValueFix.class)
.scope(InstanceScope.SCOPE_SINGLETON);
}
}
MyDialogImpl类继承的是DialogImp,代码如下:
<pre name="code" class="java">public class MyDialogImpl extends DialogImpl {
@Override
public void showToastShort(Context context, String msg) {
if (!TextUtils.isEmpty(msg)) {
super.showToastLong(context, msg);
Toast toast = IocContainer.getShare().get(Toast.class);
toast.setDuration(Toast.LENGTH_SHORT);
View view = toast.getView();
TextView text = (TextView) view.findViewById(R.id.ivory_toast_text);
if (text == null) {
View toastview = LayoutInflater.from(context).inflate(
R.layout.toast_view, null);
text = (TextView) toastview.findViewById(R.id.ivory_toast_text);
LayoutParams params = text.getLayoutParams();
<span style="color:#FF6666;">IUtil.init((Activity)context);</span>
params.width = IUtil.getDisplaywidth();
text.setLayoutParams(params);
toast.setView(toastview);
toast.setGravity(Gravity.TOP, 0, DhUtil.dip2px(context, 48));
}
text.setText(msg);
toast.show();
}
}
@Override
public void showToastLong(Context context, String msg) {
showToastShort(context, msg);
}
@Override
public void showToastType(Context context, String msg, String type) {
showToastShort(context, msg);
}
然后在其他类中注入IDialog,调用showToastShort()方法时,toast一直无法弹出,原因是没有调用上述代码中标红的代码,toast的宽度为0.
public class IUtil {
private static int displaywidth;
private static int displayHeight;
public static void init(Activity activity) {
// 获取屏幕的宽�?
Display display = activity.getWindowManager().getDefaultDisplay();
displaywidth = display.getWidth();
displayHeight = display.getHeight();
}
public static int getDisplaywidth() {
return displaywidth;
}
public static int getDisplayHeight() {
return displayHeight;
}
}
这些在dhroid的demo中写的很清楚