一段文字顏色不一樣點擊不一樣,不想寫多個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));