-
Iconfont在Android中的使用
阿里提供的Iconfont-國內(nèi)功能很強(qiáng)大且圖標(biāo)內(nèi)容很豐富的矢量圖標(biāo)庫,提供矢量圖標(biāo)下載刁俭、在線存儲(chǔ)、格式轉(zhuǎn)換等功能。
如何使用:- 從iconfont平臺(tái)選擇要使用到的圖標(biāo)
- 下載代碼,把iconfont.ttf文件導(dǎo)入到項(xiàng)目中的assets中的iconfont文件夾中
- 用TextView代替ImageView,找到圖標(biāo)相對(duì)應(yīng)的 HTML 實(shí)體字符碼給textView設(shè)置
- textview設(shè)置大小跟顏色沮焕,圖標(biāo)的大小顏色也會(huì)改變
- 為Textview設(shè)置指定的ttf文字
<TextView android:id="@+id/icon_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textColor="@color/red" android:textSize="50dp"/> //為TextView設(shè)置指定ttf文字 Typeface iconfont = Typeface.createFromAsset(getAssets(), "iconfont/iconfont.ttf"); TextView textview = (TextView)findViewById(R.id.icon_text); textview.setTypeface(iconfont);
上述方法可以使用iconfont了,但是每次都給TextView設(shè)置指定setTypeface是不是也很繁瑣,而且一直不斷的在讀取iconfont.ttf文字,也很浪費(fèi)內(nèi)存,所以就想到封裝一個(gè)工具類脸爱。代碼如下:
public class FontHelper { public static final String DEF_FONT = "iconfont/iconfont.ttf"; public static final void injectFont(View rootView) { injectFont(rootView, Typeface.createFromAsset(rootView.getContext().getAssets(), DEF_FONT)); } private static void injectFont(View rootView, Typeface typeface) { if (rootView instanceof ViewGroup) { ViewGroup viewGroup = (ViewGroup) rootView; int childViewCount = viewGroup.getChildCount(); for (int i = 0; i < childViewCount; i++) { injectFont(viewGroup.getChildAt(i), typeface); } } else if (rootView instanceof TextView) { ((TextView) rootView).setTypeface(typeface); } } }
這樣我們每次調(diào)用FontHelper.injectFont(textview)就可以了遇汞,你可能會(huì)說這還不是我想要的,我連這行代碼都不想多寫簿废,那好,接著往下看:我們可以自定義一個(gè)TextView然后初始化時(shí)setTypeface即刻络它,代碼如下:
public class TextViewIcon extends AppCompatTextView { public TextViewIcon(Context context) { super(context); init(context); } public TextViewIcon(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public TextViewIcon(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } private void init(Context context) { setTypeface(Typeface.createFromAsset(context.getAssets(),"iconfont/iconfont.ttf")); } }
現(xiàn)在我們?cè)诓季治募袑懭缦麓a即可:
<com.xxx.xxx.TextViewIcon android:id="@+id/icon_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textColor="@color/red" android:textSize="50dp"/>
可是我也想實(shí)現(xiàn)像普通textview動(dòng)態(tài)改變文字一樣動(dòng)態(tài)改變iconfont的效果怎么辦呢族檬?也就是說在代碼里動(dòng)態(tài)的調(diào)整圖標(biāo)的大小顏色或改變圖片,iconfont改變大小顏色這很簡(jiǎn)單直接調(diào)用TextView的setTextSize和setTextColor就可以了化戳,動(dòng)態(tài)設(shè)置圖片是不是setText呢单料?
textview.setText("");
你會(huì)發(fā)現(xiàn)這并不會(huì)如你所愿顯示對(duì)應(yīng)的圖片效果,因?yàn)檫@里涉及到unicode 字符的問題点楼。所以將"&#x" 替換成 "\u"扫尖,用 unicode 字符來表示,即代碼如下:
textview.settext("\ue66e");
-
Scrollview 自動(dòng)滾動(dòng)到頂部或者底部
- 設(shè)置默認(rèn)滾動(dòng)到頂部
scrollView.post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub scrollView.fullScroll(ScrollView.FOCUS_UP); } });
- 設(shè)置默認(rèn)滾動(dòng)到底部
scrollView.post(new Runnable() { @Override public void run() { // TODO Auto-generated method stub scrollView.fullScroll(ScrollView.FOCUS_DOWN); } });
- 設(shè)置默認(rèn)滾動(dòng)到頂部
-
判斷App的運(yùn)行狀態(tài)
/** * 返回app運(yùn)行狀態(tài) * 1:程序在前臺(tái)運(yùn)行 * 2:程序在后臺(tái)運(yùn)行 * 3:程序未啟動(dòng) * 注意:需要配置權(quán)限<uses-permission android:name="android.permission.GET_TASKS" /> */ public static int getAppSatus(Context context, String pageName) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(20); //判斷程序是否在棧頂 if (list.get(0).topActivity.getPackageName().equals(pageName)) { return 1; } else { //判斷程序是否在棧里 for (ActivityManager.RunningTaskInfo info : list) { if (info.topActivity.getPackageName().equals(pageName)) { return 2; } } return 3;//棧里找不到掠廓,返回3 } }
-
根據(jù)圖片寬度自適應(yīng)圖片控件的高度
/*將ImageView 根據(jù)寬度自適應(yīng)高度(此處假設(shè)寬度>高度)*/ public static void adaptiveHeightByWidth(ImageView view, int width){ ViewGroup.LayoutParams lp = view.getLayoutParams(); lp.width = width; lp.height = ViewGroup.LayoutParams.WRAP_CONTENT; view.setLayoutParams(lp); view.setMaxWidth(width); view.setMaxHeight(width); }
-
Android 中如何調(diào)試webview
- .在APP中啟用 WebView 調(diào)試换怖,開啟調(diào)試后,Chrome DevTools才能對(duì)WebView進(jìn)行遠(yuǎn)程調(diào)試蟀瞧;
在對(duì)應(yīng)的android頁面中添加如下代碼:
WebView.setWebContentsDebuggingEnabled(true);
- 通過Chrome瀏覽器訪問chrome://inspect/#devices訪問已啟用調(diào)試的 WebView 列表沉颂;
- 調(diào)試Webview與遠(yuǎn)程調(diào)試普通頁面相同,遠(yuǎn)程調(diào)試普通頁面也就是在安卓手機(jī)中安裝Chrome瀏覽器悦污,使用USB 連接 PC铸屉,然后在 PC 的 Chrome 瀏覽器中打開 chrome://inspect/#devices 即可。
- 點(diǎn)擊inspect 進(jìn)入dev_tools 頁面可能碰到404 Not Found錯(cuò)誤切端,那是因?yàn)槟阍趪鴥?nèi)彻坛,F(xiàn)Q即可。
- .在APP中啟用 WebView 調(diào)試换怖,開啟調(diào)試后,Chrome DevTools才能對(duì)WebView進(jìn)行遠(yuǎn)程調(diào)試蟀瞧;
-
Android按Home鍵避免重啟程序
問題描述:
在App打開后按Home鍵后應(yīng)用退至后臺(tái)運(yùn)行踏枣,此時(shí)點(diǎn)擊應(yīng)用圖標(biāo)再次打開App昌屉,發(fā)現(xiàn)App重新啟動(dòng),難道打開程序的頁面不應(yīng)該是在按Home鍵之前的頁面嗎椰于?
解決方案:
在啟動(dòng)頁也就是應(yīng)用程序剛進(jìn)入的Activity 即大部分都為閃屏頁怠益,在OnCreate方法中是在setContentView之前加入下面的代碼還是在setContentView之后加入一下代碼都可以。if (!this.isTaskRoot()) { Intent mainIntent = getIntent(); String action = mainIntent.getAction(); if (mainIntent.hasCategory(Intent.CATEGORY_LAUNCHER) && action.equals(Intent.ACTION_MAIN)) { finish(); return; } }
-
Android中使用Glide獲取網(wǎng)絡(luò)圖片的寬高比
public class ImageUtil { /** * 根據(jù)圖片鏈接獲取圖片寬高比 * @param context * @param imgUrl * @return 高/寬 */ public static void getAspectRatioByImgUrl(Context context, String imgUrl, ImageAspectRatioCallBack aspectRatioCallBack){ if (TextUtils.isEmpty(imgUrl)){ return; } //獲取圖片真正的寬高 Glide.with(context) .load(imgUrl) .asBitmap()//強(qiáng)制Glide返回一個(gè)Bitmap對(duì)象 .into(new SimpleTarget<Bitmap>() { @Override public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); Log.e(Constant.LOG_TAG, "width " + width); Log.e(Constant.LOG_TAG, "height " + height); if (width > 0 && aspectRatioCallBack != null){ aspectRatioCallBack.returnAspectRation((float) height/width); } } }); } public interface ImageAspectRatioCallBack { void returnAspectRation(float aspectRatio); } }
-
Android中webview嵌套H5頁面報(bào)錯(cuò)
"Uncaught Error: Java exception was raised during method invocation"
問題描述:
客戶端提供給H5一個(gè)方法供其修改原生頁面標(biāo)題的頭部顯示信息瘾婿,代碼如下:@JavascriptInterface public void resetTitle(String title2) { mTitleTv.setText(title2); }
結(jié)果H5在調(diào)用該方法時(shí)就會(huì)報(bào)此錯(cuò)誤蜻牢,原因是因?yàn)?strong>在
@JavascriptInterface
注解的方法里面不能更新ui.
解決方案:@JavascriptInterface public void resetTitle(String title2) { BrowserActivity.this.runOnUiThread(new Runnable() { @Override public void run() { mTitleTv.setText(title2); } }); }
查看android手機(jī)中安裝apk的包名等信息
- 方法一:
進(jìn)入cmd窗口烤咧,輸入adb shell,進(jìn)入手機(jī)抢呆,在輸入ls /data/data煮嫌,即能顯示出手機(jī)中安裝apk的包名。(需要root權(quán)限) - 方法二:
查看手機(jī)中非系統(tǒng)的apk包名信息抱虐,adb shell pm list package -3昌阿,這個(gè)命令很實(shí)用。這和命令后面不加-3表示查看手機(jī)中使用的apk包名恳邀。 - 方法三:
在代碼中獲取Android設(shè)備中apk的包名等信息懦冰。/*獲取Android手機(jī)中安裝的應(yīng)用*/ public static void getAllAppPackages(Context context) { Intent intent = new Intent(Intent.ACTION_MAIN, null); intent.addCategory(Intent.CATEGORY_LAUNCHER); List<ResolveInfo> apps = context.getPackageManager().queryIntentActivities(intent, 0); //for循環(huán)遍歷ResolveInfo對(duì)象獲取包名和類名 for (int i = 0; i < apps.size(); i++) { ResolveInfo info = apps.get(i); String packageName = info.activityInfo.packageName; CharSequence cls = info.activityInfo.name; CharSequence name = info.activityInfo.loadLabel(context.getPackageManager()); Log.e(Constant.LOG_TAG,name+"----"+packageName+"----"+cls); } }
-
解決使用HttpURLConnection讀取網(wǎng)絡(luò)文件亂碼問題
public static void getRemoteMockApiData(final String mockdataUrl, final JsonCallBack jsonCallBack){ final StringBuilder stringBuilder = new StringBuilder(); new Thread(new Runnable() { @Override public void run() { try { //構(gòu)建URL地址 URL url = new URL(mockdataUrl); try { HttpURLConnection hcont = (HttpURLConnection) url.openConnection(); hcont.setRequestProperty("Content-Type", "application/json"); hcont.setDoOutput(true); hcont.setRequestMethod("GET"); hcont.setRequestProperty("Accept-Charset", "utf-8"); hcont.setRequestProperty("contentType", "utf-8"); if (hcont.getResponseCode() == 200) { InputStreamReader in = null; in = new InputStreamReader(hcont.getInputStream(),"utf-8"); BufferedReader bufferedReader = new BufferedReader(in); String line = null; while ((line = bufferedReader.readLine()) != null) { stringBuilder.append(line); } } if (jsonCallBack != null){ jsonCallBack.getRemoteMockData(stringBuilder.toString()); } } catch (IOException e) { e.printStackTrace(); } } catch (MalformedURLException e) { e.printStackTrace(); } } }).start(); }
-
基于android studio 3.1.4將library生成jar和arr文件
其實(shí)Android Studio編譯的時(shí)候會(huì)自動(dòng)將library項(xiàng)目生成jar和aar的,分別在build/outputs/aar
和build/intermediates/packaged-classes
文件夾下谣沸。
-
在對(duì)應(yīng)的module文件下的build.gradle中添加以下代碼:
task makeJar(type: Copy) { //刪除存在的 delete 'build/libs/react-native-bridge.jar' //設(shè)置拷貝的文件 from('build/intermediates/packaged-classes/release/') //打進(jìn)jar包后的文件目錄 into('build/libs/') //將classes.jar放入build/libs/目錄下 include('classes.jar') rename ('classes.jar','react-native-bridge.jar') } makeJar.dependsOn(build)
- 選中l(wèi)ibrary的Module刷钢,點(diǎn)擊Build -> Make Module library,此時(shí)packaged-classes目錄即可生成乳附。
-
在AS最下方的Terminal中輸入命令即可内地。
- MAC電腦:
./gradlew makeJar
- Windows電腦:
gradlew makeJar
- MAC電腦:
- 順利的話就可以在
build/libs
文件夾下看到生成的jar文件。
ADB命令:
- 獲取手機(jī)CPU型號(hào):
adb shell getprop ro.product.cpu.abi
- 獲取當(dāng)前棧頂Activity相關(guān)的信息:
adb shell dumpsys activity top > info.txt