一、用代码设置输入框最大输入长度:
// 设置最大长度为8
phoneEdtTxt.setFilters(new InputFilter[]{new InputFilter.LengthFilter(8)});
二、一段字符多种颜色设置:
SpannableString spannableString = new SpannableString(content);
ForegroundColorSpan colorSpan = new ForegroundColorSpan(mContext.getResources()
.getColor(R.color.textColor_A2));
spannableString.setSpan(colorSpan, 0, startTxt.length(),
Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
mTextView.setText(spannableString);
三、指定位置设置图片:
Drawable right = mContext.getResources().getDrawable(R.drawable.ic_tick);
right.setBounds(0, 0, right.getMinimumWidth(), right.getMinimumHeight());
mTextView.setCompoundDrawables(null, null, right, null);
四、指定位置弹出PopupWindow(注:在Android 7.0 + 上PopupWindow的高度一定要设置为 WRAP_CONTENT,否则无法在指定位置显示):
if (Build.VERSION.SDK_INT < 24)
{
dropListPopupWindow.showAsDropDown(this, 0, 5);
}
else
{
// 适配 android 7.0 +
int[] location = new int[2];
getLocationOnScreen(location);
int x = location[0];
int y = location[1];
Log.e(getClass().getSimpleName(), "x : " + x + ", y : " + y);
dropListPopupWindow.showAtLocation(this, Gravity.NO_GRAVITY, 0,
y + getHeight() + 5);
}
五、设置window:
WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth(); // 获取屏幕宽、高用
Window dialogWindow = dialog.getWindow();
WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
p.width = (int) (width * 0.8); // 宽度设置为屏幕的0.8
dialogWindow.setAttributes(p);
六、获取控件在屏幕的位置(x,y坐标):
int[] location = new int[2];
getLocationOnScreen(location);
int x = location[0];
int y = location[1];
Log.e(getClass().getSimpleName(), "x : " + x + ", y : " + y);
七、获取屏幕宽、高:
// 方法一
View decorView = getWindow().getDecorView();
decorView.post(() -> {
// 1
// width : 1080, height : 1920
Log.e(TAG, "width : " + decorView.getWidth() + ", height : " + decorView.getHeight());
// measuredwidth : 1080, measuredheight : 1920
Log.e(TAG, "measuredwidth : " + decorView.getMeasuredWidth() + ", measuredheight : " + decorView.getMeasuredHeight());
// 2
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
{
DisplayMetrics displayMetrics = new DisplayMetrics();
decorView.getDisplay().getMetrics(displayMetrics);
// DisplayMetrics{density=3.0, width=1080, height=1920, scaledDensity=3.0, xdpi=428.625, ydpi=427.789}
Log.e(TAG, displayMetrics.toString());
}
});
// 方法二
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
// WindowManager, width : 1080, height : 1920
Log.e(TAG, "WindowManager, width : " + display.getWidth() + ", height : " + display.getHeight());
// 方法三
WindowManager wm = getWindowManager();
DisplayMetrics displayMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(displayMetrics);
// WindowManager : DisplayMetrics{density=3.0, width=1080, height=1920, scaledDensity=3.0, xdpi=428.625, ydpi=427.789}
Log.e(TAG, "WindowManager : " + displayMetrics.toString());
// 方法四
Resources resources = getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
// Resources : DisplayMetrics{density=3.0, width=1080, height=1920, scaledDensity=3.0, xdpi=428.625, ydpi=427.789}
Log.e(TAG, "Resources : " + metrics.toString());
网友评论