这篇博客是用来记录自己在写代码的过程中遇到的一些问题,以及解决方法,做一个总结,算是笔记吧。
1.问题描述:
以某一触发唤醒屏幕
解决方案:
public static void wakeUpAndUnlock(Context context){
KeyguardManager km= (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock");
// 解锁
kl.disableKeyguard();
// 获取电源管理器对象
PowerManager pm=(PowerManager) context.getSystemService(Context.POWER_SERVICE);
// 获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");
// 点亮屏幕
wl.acquire();
// 释放
wl.release();
}注:其中,解锁步骤可以省略。这种情况下,就只是点亮你的屏幕而不会解锁了。
2.问题描述:
除掉Actionbar最左侧的箭头注意,这个最左侧的的箭头可不是actionbar的icon。如果你去设置它的icon,这样就会导致既有icon也有箭头。
解决方案:
getActionBar().setHomeAsUpIndicator(drawable);上面的这种方法是适用于API18,而对于API小于18的代码则是不适用的。需要做更多的逻辑。如下:
public static void changeActionBarHomeUpDrawable(Activity activity, int rid) {
Drawable homeUp = activity.getResources().getDrawable(rid);
final View home = activity.findViewById(android.R.id.home);
if (home == null) {
// Action bar doesn't have a known configuration, an OEM messed with things.
return;
}
final ViewGroup parent = (ViewGroup) home.getParent();
final int childCount = parent.getChildCount();
if (childCount != 2) {
// No idea which one will be the right one, an OEM messed with things.
return;
}
final View first = parent.getChildAt(0);
final View second = parent.getChildAt(1);
final View up = first.getId() == android.R.id.home ? second : first;
if (up instanceof ImageView) {
ImageView upIndicatorView = (ImageView) up;
upIndicatorView.setImageDrawable(homeUp);
}
}
3.问题描述:
Android Private Libraries缺失如果你的Android Private Libraries是你误删除的话,你可以通过下面的方法1恢复。如果你的项目无原无故的缺失了Android Private Libraries,那么你可以使用下面的方法2修复。
1. 在项目上点击右键,点击Android Tools -> Fix Project Properties 即可
2. android-support-v4重复,删除一个即可
4.问题描述:
Arrays.copyOfRange在低版本的Android中出现NoSuchMethodError异常对于这个问题的解释,可能要归结于API版本之间的兼容性了。
解决方案:
我的解决方法就是直接重写了这个方法。非常简单粗暴的一个方法,而且很有效。下面是对于Array的一些常用方法的示例源码:
http://commons.apache.org/proper/commons-lang/apidocs/src-html/org/apache/commons/lang3/ArrayUtils.html
5.问题描述:
ListView与动态显示的底部操作栏你是否遇到过这样的一个问题:你有一个始终位于屏幕底部的Layout,还有一个ListView,它是始终位于屏幕的顶部。当ListView的item过多时,下面的部分item就会被拦住,或是把底部的Layout拦住。
解决方案:
对于上面的问题,可能你已经尝试过,使用让Listview位于Layout上方,或是ListView同时位于屏幕顶部和Layout上方。可是尝试过后就会是沮丧的。下面我就分享一个我的技巧:在Layout的外部再套一层Layout,高度为wrap_content。如下:
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/container_relativeLayout"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
</ListView>
<RelativeLayout
android:id="@+id/container_relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bottom Layout" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
注:可能你会觉得这样是多此一举。因为我可以把ListView的高度设置成match_parent或是fill_parent,再位于Layout之上。这种方式只是适合于,Layout不会被Gone的情况。这里是一个值得注意的地方。
大家还可以参见一些Android其他的细节总结:
Android细节问题总结(一)