首页 > 系统 > Android > 正文

Android 小方法

2019-11-08 00:30:09
字体:
来源:转载
供稿:网友
根据图片名获取图片id public static int getDrawbleId(Context context, String name) { return context.getResources().getIdentifier(name, "drawable", context.getPackageName()); }获取textView是否被截断 stackoverflow 在ViewHolder中不调用onGlobalLayoutListener,使用onPReDrawListener textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (Build.VERSION.SDK_INT >= 16) { textView.getViewTreeObserver().removeOnGlobalLayoutListener(this); }else { textView.getViewTreeObserver().removeGlobalOnLayoutListener(this); } Layout l = textView.getLayout(); if (l != null) { int lines = l.getLineCount(); if (lines > 0) if (l.getEllipsisCount(lines - 1) > 0) LogUtil.d("Text is ellipsized"); } } });ListView模拟点击 stackoverflowlv_left.performItemClick(mAdapter.getView(position, null, null), position, mAdapter.getItemId(position));判断当前线程是否为主线程if (Looper.myLooper() == Looper.getMainLooper()){ //...}设置和获取Actionbar高度if(getContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { int actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());}//在style中配置<item name="android:actionBarSize">@dimen/actionBar_size</item><item name="actionBarSize">@dimen/actionBar_size</item>Fragment 展出、消失动画getFragmentManager().beginTransaction() //设置动画要在add之前 .setCustomAnimations(R.anim.left_to_right,R.anim.right_to_left, R.anim.left_to_right, R.anim.right_to_left) .add(R.id.mainContainer, Fragment.instantiate(mContext, name), "TAG") .addToBackStack(null) .commitAllowingStateLoss();遍历SpareArray// 用keyAt(int index)获取索引或者valueAt(int index)获取值int key = 0;for(int i = 0,size = sparseArray.size(); i < size; i++) { key = sparseArray.keyAt(i); Object obj = sparseArray.get(key);}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表