有时候我们需要对网络状态,无数据等情况,添加一个统一的样式.如果直接添加到代码或者xml中,则每个布局都需要写.而且因为每个布局都不一样,导致所有的页面的操作方式都不相同.
这里提供一个通用的替换方法,可以很方便的替换掉指定的View
/**
* @方法名: replaceView
* @功能描述:替换View,替换成指定的View
* @param ac
* 当前Activity
* @param id
* 需要替换的View的id
* @param view
* 替换View
* @return void
*/
public static void replaceView(FragmentActivity ac, int id, View view) {
if (ac == null) {
Log.e("siyehua-status", "Activity has been destory!");
return;
}
View rootView = ac.findViewById(android.R.id.content);
if (rootView != null && rootView instanceof FrameLayout) {
FrameLayout rootGroup = (FrameLayout) rootView;
View childView = rootGroup.findViewById(id);
if (childView != null) {// 如果有这个View
rootGroup.addView(view);
FrameLayout.LayoutParams layoutParams = (LayoutParams) view
.getLayoutParams();
layoutParams.width = childView.getWidth();// 设置被添加进来的View的宽度
layoutParams.height = childView.getHeight();// 设置被添加进来的View的高度
layoutParams.setMargins(childView.getLeft(),
childView.getTop(), 0, 0);// 设置被添加进来的View的位置
view.setLayoutParams(layoutParams);
} else {
Log.e("siyehua-status",
"The view isn't exist which you want to replaced! Please check the view's id.");
}
} else {
Log.e("siyehua-status",
"The rootView isn't exist! Please check the rootView's id in code.");
}
}
/**
* @方法名: remmoveReplaceView
* @功能描述:移除替换的View
* @param ac
* 当前Activity
* @param view
* 上次被添加进来的View
* @return void
*/
public static void remmoveReplaceView(FragmentActivity ac, View view) {
if (ac == null) {
Log.e("siyehua-status", "Activity has been destory!");
return;
}
View rootView = ac.findViewById(android.R.id.content);
if (rootView != null && rootView instanceof FrameLayout) {
FrameLayout rootGroup = (FrameLayout) rootView;
rootGroup.removeView(view);
} else {
Log.e("siyehua-status",
"The rootView isn't exist! Please check the rootView's id in code.");
}
}
private static TextView toastView;
/**
* @方法名: replaceToastView
* @功能描述:在View中添加提示
* @param ac
* 当前Activity
* @param id
* 需要被替换的view的id
* @param content
* 添加的文字
* @param textSize
* 文字的大小
* @param textColor
* 文字的颜色
* @return void
*/
public static void replaceToastView(FragmentActivity ac, int id,
String content, int textSize, int textColor) {
if (ac == null) {
Log.e("siyehua-status", "Activity has been destory!");
return;
}
View rootView = ac.findViewById(android.R.id.content);
if (rootView != null && rootView instanceof FrameLayout) {
FrameLayout rootGroup = (FrameLayout) rootView;
View childView = rootGroup.findViewById(id);
if (childView != null) {// 如果有这个View
int centerY = childView.getBottom() - childView.getHeight() / 2;// 得到当前View的中心点Y
int centerX = childView.getRight() - childView.getWidth() / 2;// 得到当前View的中心点X
if (toastView == null) {
toastView = new TextView(ac);
toastView.setIncludeFontPadding(false);
toastView.setGravity(Gravity.CENTER);
rootGroup.addView(toastView);
FrameLayout.LayoutParams layoutParams = (LayoutParams) toastView
.getLayoutParams();
layoutParams.width = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
layoutParams.height = android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
int textWidth = (int) toastView.getPaint().measureText(
content);// 得到字体宽度
Rect textBounds = new Rect();
toastView.getPaint().getTextBounds(content, 0,
content.length(), textBounds);
int textHeight = textBounds.bottom - textBounds.top;// 得到字体的高度
layoutParams.setMargins(centerX - textWidth / 2, centerY
- textHeight / 2, 0, 0);// 设置TextView的位置(中心与需要替换的View的中心一致)
toastView.setLayoutParams(layoutParams);
}
toastView.setText(content);
toastView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
toastView.setTextColor(textColor);
} else {
Log.e("siyehua-status",
"The view isn't exist which you want to replaced! Please check the view's id.");
}
} else {
Log.e("siyehua-status",
"The rootView isn't exist! Please check the rootView's id in code.");
}
}
/**
* @方法名: removeToastView
* @功能描述:移除添加的文字View
* @param ac
* 当前Activity
* @return void
*/
public static void removeToastView(FragmentActivity ac) {
if (ac == null) {
Log.e("siyehua-status", "Activity has been destory!");
return;
}
if (toastView != null) {
View rootView = ac.findViewById(android.R.id.content);
if (rootView != null && rootView instanceof FrameLayout) {
FrameLayout rootGroup = (FrameLayout) rootView;
rootGroup.removeView(toastView);
toastView = null;
} else {
Log.e("siyehua-status",
"The rootView isn't exist! Please check the rootView's id in code.");
}
}
}
不足:
当有多个提示内容时,使用的是同一个Textview
可以把静态方法和属性写成非静态的.