- 通过Drawable的方式改变shap背景颜色
// 以 shap 作为背景,改变 shap 的背景颜色
TextView test = (TextView) findViewById(R.id.tv_test);
GradientDrawable drawable = (GradientDrawable) test.getBackground();
drawable.setColor(Color.GREEN);
drawable.setAlpha(120);
test.setBackgroundDrawable(drawable);
- 设置状态栏的高度
<View
android:id="@+id/status_line"
android:layout_width="match_parent"
android:layout_height="0dp" />
int statusBarHeight = mContext.getResources().getDimensionPixelSize
(mContext.getResources().getIdentifier("status_bar_height", "dimen", "android"));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight);
mTitleTop.setLayoutParams(params);
- RecyclerView 屏蔽滑动
解决ScrollView嵌套RecyclerView获得焦点问题
LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext) {
@Override
public boolean canScrollVertically() {
return false;
}
};
- 屏蔽listview的焦点
listview.setFocusable(false);
- 获得自定义View宽高
/**
* 获取到布局文件的高度和宽度
*
* @param child
*/
private void measureView(View child) {
ViewGroup.LayoutParams lp = child.getLayoutParams();
if (lp == null) {
lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
int childMeasureWidth = ViewGroup.getChildMeasureSpec(0, 0, lp.width);
int childMeasureHeight;
if (lp.height > 0) {
childMeasureHeight = View.MeasureSpec.makeMeasureSpec(lp.height, View.MeasureSpec.EXACTLY);
} else {
childMeasureHeight = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
}
child.measure(childMeasureWidth, childMeasureHeight);
}
应用
设置 view measureView(自定义view);
高 childView.getMeasuredHeight()
宽 childView.getMeasuredWidth()
- 反射得到资源
/**
* 反射得到资源id
*
* @param context
* @param key
* 传入参数,如"R.drawable.ic_launcher", "R.id.textview"
* @return
*/
public static int getResourceId(Context context, String key) {
String[] keys = key.split("\\.");
Resources res = context.getResources();
return res.getIdentifier(keys[2], keys[1], context.getPackageName());
}
- 判断多次点击
private static long lastClickTime = 0;
private static final long DIFF = 800;
/**
* 判断两次点击的间隔,如果小于diff,则认为是多次无效点击
*
* @return
*/
public boolean isFastDoubleClick() {
long time = System.currentTimeMillis();
long timeD = time - lastClickTime;
lastClickTime = time;
if (timeD < DIFF) {
return true;
}
return false;
}
- FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS:
如果调出的Activtivity只是一个功能片段,并没有实际的意义,也没有必要出现在长按Home键调出最近使用过的程序类表中,那么使用FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
Intent intent = new Intent(this, WaitingFallBackDialog.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(intent);