一段文字颜色不一样点击不一样,不想写多个textview,能够这样写html
String html="仅限<font color=#FF9900>"+offer.getCardGroupName()+"</font>使用"; nrbankTextView.setText(Html.fromHtml(html));
也能够这样写android
String content = "发现" + newGroup.size() + "张新卡片"; int textColor = getResources().getColor(R.color.import_card_red); SpannableString spannableString = TextStyleUtils.setTextStyle(content, newGroup.size() + "张", textColor); tv_.setText(spannableString);
也能够这样写:程序员
private void setCurrentTip() { StringBuilder actionText = new StringBuilder(); actionText.append("<a style=\"text-decoration:none;\" href='text' >若平台今日快速还款额度用完,还款将于第二天到帐\n</a>"); actionText.append("<a style=\"text-decoration:none;\" href='text' >查看</a>"); actionText.append("<a style=\"text-decoration:none;\" href='blue' >规则</a>"); tvBankArrivaldesc.setText(Html.fromHtml(actionText.toString())); tvBankArrivaldesc.setMovementMethod(LinkMovementMethod .getInstance()); CharSequence text = tvBankArrivaldesc.getText(); int ends = text.length(); Spannable spannable = (Spannable) tvBankArrivaldesc.getText(); URLSpan[] urlspan = spannable.getSpans(0, ends, URLSpan.class); SpannableStringBuilder stylesBuilder = new SpannableStringBuilder(text); stylesBuilder.clearSpans(); for (URLSpan url : urlspan) { FeedTextViewURLSpan myURLSpan = new FeedTextViewURLSpan(url.getURL(), getActivity()); stylesBuilder.setSpan(myURLSpan, spannable.getSpanStart(url), spannable.getSpanEnd(url), spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } tvBankArrivaldesc.setText(stylesBuilder); } static class FeedTextViewURLSpan extends ClickableSpan { private String clickString; private Context context; public FeedTextViewURLSpan(String clickString, Context context) { this.clickString = clickString; this.context = context; } @Override public void updateDrawState(TextPaint ds) { ds.setUnderlineText(false);//去掉下划线 //给标记的部分 的文字 添加颜色 if (clickString.equals("blue")) { ds.setColor(Color.parseColor("#4D8FDD")); } else if (clickString.equals("text")) { ds.setColor(Color.parseColor("#999999")); } } @Override public void onClick(View widget) { Intent intent = new Intent(); // 根据文字的标记 来进行相应的 响应事件 if (clickString.equals("blue")) { ToastUtils.show(context, "规则"); } } }
想每次进入页面刷新,能够放在onResume方法里(不推荐)
颜色透明度:#7f000000 表明50%透明度的黑色
web
ImageView的android:adjustViewBounds属性
http://blog.csdn.net/pingchuanyang/article/details/9252689数据库
framelayout两布局重叠,如何让下层不响应事件
在layout文件里本层下增长Android:clickable="true"
在上层布局的父布局上增长android:clickable="true"api
给图片设置背景,例如ui给的图背景透明的图片数组
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape> <solid android:color="#1c69ac" /> <corners android:radius="5dp"/> </shape> </item> <item android:left="15dp" android:right="15dp" android:top="15dp" android:bottom="15dp" android:drawable="@drawable/icon_yingwen_fuhao_keyboard_del_default" /> </layer-list>
因为listview的复用机制,当给一个控制设置状态的时候,相应的要给其余控件也设置状态(else),不然就会复用
缓存
listview若是单独更新某个控件的时候,能够更改单个数据的状态,而后notify
安全
viewpager+fragment服务器
//界面可见时再加载数据 @Override public void setUserVisibleHint(boolean isVisibleToUser{ super.setUserVisibleHint(isVisibleToUser); if (isVisibleToUser) { mMyWalletPresenter.reqRanking(position);//[2周排行,1总排行] } }
CardView
使用android:background设置背景颜色无效。 app:cardBackgroundColor 设置背景颜色
NestedScrollView嵌套RecyclerView滑动卡顿解决方案
若是你APP的API适配的minSdkVersion高于21,直接在RecyclerView中加上android:nestedScrollingEnabled="false"或者禁用recycleview滑动
//解决滑动滑动冲突 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false) { @Override public boolean canScrollVertically() { return false; } };
selector异常
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #3: <item> tag requires a 'drawable' attribute or child tag defining a drawable
产生缘由:个人一个button按钮的background属性中设置成"@color/button_text_selector",按照异常来讲,这个background这个属性的值必须是drawable类型的,不能是color类型。
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!--<item android:color="@color/iqb_ui_base_color_1004" android:state_enabled="true" />--> <!--<item android:color="@color/iqb_ui_base_color_1005" android:state_enabled="false" />--> <!--<item android:color="@color/iqb_ui_base_color_1004" />--> <item android:state_enabled="true"> <shape> <solid android:color="@color/iqb_ui_base_color_1004" /> </shape> </item> <item android:state_enabled="false"> <shape> <solid android:color="@color/iqb_ui_base_color_1005" /> </shape> </item> </selector>
代码写阴影
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 阴影部分 --> <!-- 我的以为更形象的表达:top表明下边的阴影高度,left表明右边的阴影宽度。其实也就是相对应的offset,solid中的颜色是阴影的颜色,也能够设置角度等等 --> <item android:bottom="2dp" android:left="2dp" android:right="2dp" android:top="2dp"> <shape android:shape="rectangle"> <gradient android:angle="270" android:endColor="#0F000000" android:startColor="#0F000000" /> <corners android:bottomLeftRadius="6dip" android:bottomRightRadius="6dip" android:topLeftRadius="6dip" android:topRightRadius="6dip" /> </shape> </item> <!-- 背景部分 --> <!-- 形象的表达:bottom表明背景部分在上边缘超出阴影的高度,right表明背景部分在左边超出阴影的宽度(相对应的offset) --> <item android:bottom="5dp" android:left="3dp" android:right="3dp" android:top="3dp"> <shape android:shape="rectangle"> <gradient android:angle="270" android:endColor="#FFFFFF" android:startColor="#FFFFFF" /> <corners android:bottomLeftRadius="6dip" android:bottomRightRadius="6dip" android:topLeftRadius="6dip" android:topRightRadius="6dip" /> </shape> </item> </layer-list>
单选
public void setClicked(int clicked) { mapClassfiyAdapter.setClicked(clicked); } public void setClicked(int clicked){ this.clicked=clicked; notifyDataSetChanged(); } if(i==clicked){ tv_classfiy.setTextColor(mContext.getResources().getColor(R.color.map_pop_click)); } else { tv_classfiy.setTextColor(mContext.getResources().getColor(R.color.white)); }
多选
// 用来控制CheckBox的选中情况 private static HashMap<Integer, Boolean> isSelected; public MapClassfiyAdapter(Context mContext, List<String> classfiyList) { this.mContext = mContext; isSelected = new HashMap<Integer, Boolean>(); } // 初始化isSelected的数据 private void initDate() { for (int i = 0; i < classfiyList.size(); i++) { getIsSelected().put(i, false); } } public HashMap<Integer, Boolean> getIsSelected() { return isSelected; } public void setIsSelected(HashMap<Integer, Boolean> isSelected) { MapClassfiyAdapter.isSelected = isSelected; } @Override public View getView(int i, View convertView, ViewGroup viewGroup) { ... if(getIsSelected().get(i)){ tv_classfiy.setTextColor(mContext.getResources().getColor(R.color.map_pop_click)); }else{ tv_classfiy.setTextColor(mContext.getResources().getColor(R.color.white)); } tv_classfiy.setText(classfiyList.get(i)); } --- public void setClicked(int clicked) { isSelected = mapClassfiyAdapter.getIsSelected(); Boolean aBoolean = isSelected.get(clicked); if(aBoolean){ isSelected.put(clicked,false); }else{ isSelected.put(clicked,true); } mapClassfiyAdapter.setIsSelected(isSelected); }
描边
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:left="-2dp" android:right="-2dp"> <shape> <solid android:color="@color/white" /> <stroke android:width="1.5dp" android:color="#80ffffff" /> </shape> </item> </layer-list> --- <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:right="-2dp"//负数,不描右边 > <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#EFEFEF"/> <corners android:topLeftRadius="10dp" android:topRightRadius="0dp" android:bottomRightRadius="0dp" android:bottomLeftRadius="10dp" /> <stroke android:width="0.5px" android:color="#505050"/> </shape> </item> </layer-list>
popWindow点背景消失
mPopupWindow = new PopupWindow( mPopupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); //产生背景变暗效果,布局背景要改 // 根布局背景android:background="#00ffffff" backgroundAlpha(0.5f); mPopupWindow.setOutsideTouchable(true); mPopupWindow.setFocusable(true); mPopupWindow.setBackgroundDrawable(new BitmapDrawable()); mPopupWindow.setOnDismissListener(new PoponDismissListener()); mMyHandler.postDelayed(new Runnable() { @Override public void run() { mPopupWindow.showAtLocation(mPopupView, Gravity.CENTER, 0, 0); } }, 200); ... class PoponDismissListener implements PopupWindow.OnDismissListener { @Override public void onDismiss() { backgroundAlpha(1f); } } private void backgroundAlpha(float v) { WindowManager.LayoutParams lp = context.getWindow().getAttributes(); lp.alpha = v; //0.0-1.0 context.getWindow().setAttributes(lp); }
数据库升级
新增版本号,而后
if (newVersion == 4) {//方法一 TableUtils.dropTable(connectionSource, CardModel.class, true); TableUtils.createTable(connectionSource, CardModel.class); return; } if (oldVersion < 5) {//方法二 getUserInfoDao().executeRaw("ALTER TABLE `USERINFOTABLE` ADD COLUMN memberaccesstoken TEXT DEFAULT'';"); getUserInfoDao().executeRaw("ALTER TABLE `USERINFOTABLE` ADD COLUMN membertype INTEGER DEFAULT 0;"); return; }
数字后加2个点
DecimalFormat df = new DecimalFormat("########0.00"); crt_pp_money.setText("¥" + df.format(Double.valueOf(money)));
填充字符串
<string name="crt_shift_limit">银行单笔限额%1$d万,单日限额号%2$d万</string> bankMessage.setText(getResources().getString(R.string.crt_shift_limit,3,5));
webview post
webView.postUrl(mUrl,postdata.getBytes());
webview重定向引发的问题
解决方法:
WebView.HitTestResult hitTestResult = webView.getHitTestResult(); Log.d("hitTestResult",hitTestResult.getType()+"----"+hitTestResult.getExtra()); if (hitTestResult.getType() == WebView.HitTestResult.UNKNOWN_TYPE) {//=0发生重定向 loadUrl(); return true; }else{}
或者
@Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); mIsPageLoading= false;//重定向是不回执行finished方法的 } public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); mIsPageLoading= true; }
webview:显示404等异常状态都有回调,能够显示一个友好的界面
能够经过URL是否包含某个字段判断去哪一个页面
try-catch 捕获异常能够捕获里return出去
手机适配
String PhoneName = String.valueOf(android.os.Build.MANUFACTURER); if (PhoneName.equals("OPPO")) { AutoLogin1.setVisibility(View.INVISIBLE); AutoLogin2.setVisibility(View.INVISIBLE); }
华为等虚拟键盘遮挡问题
//由于某些机型是虚拟按键的,因此要加上如下设置防止挡住按键. setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
若是你触发了一个intent,并且没有任何一个app会去接收这个intent,那么你的app会crash。为了验证是否有合适的activity会响应这个intent,须要执行queryIntentActivities() 来获取到可以接收这个intent的全部activity的list。若是返回的List非空,那么你才能够安全的使用这个intent。例如:
PackageManager packageManager = getPackageManager(); List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0); boolean isIntentSafe = activities.size() > 0; if (isIntentSafe) { startActivity(mapIntent); }
检查是否有某个权限
public static boolean hasPermissions(Context context, String... perms) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { return true; } for (String perm : perms) { //PERMISSION_DENIED boolean hasPerm = (ContextCompat.checkSelfPermission(context, perm) == PackageManager.PERMISSION_GRANTED); if (!hasPerm) { return false; } } return true; } boolean location = hasPermissions(this, Manifest.permission.ACCESS_COARSE_LOCATION);
跳转到别的APP
private void doStartApplicationWithPackageName(String packagename) { Intent intent = new Intent(); PackageInfo packageinfo = null; PackageManager packageManager = context.getPackageManager(); try { packageinfo = packageManager.getPackageInfo(packagename, 0); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } if (packageinfo == null) { intent.setClass(context, NoLianshuaActivity.class); context.startActivity(intent); dismiss(); return; } intent = new Intent(Intent.ACTION_VIEW, Uri.parse("mydemo://go?money=" + money + "&isFromMydemo=" + true)); }
<activity android:name=".Main2Activity"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="mydemo" /> </intent-filter> </activity>
另外一个APP
private void parseIntentUrl() { Intent intent = getIntent(); String action = intent.getAction(); if (Intent.ACTION_VIEW.equals(action)) { Uri uri = intent.getData(); if (uri != null) { String host = uri.getHost(); String dataString = intent.getDataString(); String id = uri.getQueryParameter("goodsId"); String path = uri.getPath(); String path1 = uri.getEncodedPath(); String queryString = uri.getQuery(); Log.d("Alex", "host:"+host); Log.d("Alex", "dataString:" + dataString); Log.d("Alex", "id:" + id); Log.d("Alex", "path:" + path); Log.d("Alex", "path1:" + path1); Log.d("Alex", "queryString:" + queryString); } } }
这样写bean的字段,字段是new_p也能解析
@SerializedName("new_p")
private String psw;
ViewPager越界
ViewPager的子控件个数(getChildCount())最多为3(在destroyItem()中调用了removeView()),若是要获取ViewPager某个子页面,千万不能使用getChildAt(position),会形成数组越界。能够经过这样来获取。
@Override public Object instantiateItem(ViewGroup container, int position) { view.setTag(DEFAULT_TAG + position); container.addView(view); return view; } View child = mViewPager.findViewWithTag(DEFAULT_TAG + position));