Android进阶之代码应用技巧
博客出自:http://blog.csdn.net/liuxian13183,转载注明出处!
All Rights Reserved !
1、给文字加下划线
email_name.setText(Html.fromHtml("<u>" + email.title + "</u>"));
2、文字图片一起排,并加以间隔
android:drawableLeft="@drawable/user_id"
android:drawablePadding="10dip"
3、获得手机串号
TelephonyManager telephonyManager = (TelephonyManager) MarketApplication.sContext
.getSystemService(Context.TELEPHONY_SERVICE);
String imei = telephonyManager.getDeviceId();
4、改变tabwidget背景色
tabWidget.getChildAt(i).setBackgroundResource(
R.drawable.tab_bar_mask3);
5、去掉tabhost下的白线
tabHost.setPadding(tabHost.getPaddingLeft(), tabHost.getPaddingTop(),
tabHost.getPaddingRight(), tabHost.getPaddingBottom() - 10);
6、获得View在控件的上下左右间距
int[] loc = new int[2];
// if (fromWindowsLoc) {
// anchor.getLocationInWindow(loc);
// } else {
anchor.getLocationOnScreen(loc);
// // }
int l = loc[0];//左
int t = loc[1];//上
// 组件上下:445:474
int r = l + anchor.getWidth();// 右
int b = t + anchor.getHeight();// 下
Rect localRect = new Rect(l, t, r, b);
7、获得sd卡上的图片,并加载
ImageView.setImageUri(Uri.fromFile(new File("/sdcard/cats.jpg")));
8、解析文字
URLDecoder.decode(searchkeyword, "gb2312")
9、Activity里嵌入Activity
ll.removeAllViews();
Intent it = new Intent(new Intent(SearchActivity.this,
SearchAndroidList.class));
it.putExtra(AppData.PAPER_QUERY, query);
ll.addView(getLocalActivityManager().startActivity(
"SearchAndroidList",
it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView());
10、android 屏幕常亮保持
相关的变量:
PowerManager powerManager = null;
WakeLock wakeLock = null;
初始化:
powerManager = (PowerManager) this
.getSystemService(Service.POWER_SERVICE);
wakeLock = this.powerManager.newWakeLock(
PowerManager.SCREEN_DIM_WAKE_LOCK, "My Lock");
开启屏幕常亮:
wakeLock.setReferenceCounted(false);
wakeLock.acquire();
关闭你的程序设置的屏幕常亮:
wakeLock.release();
注意的地方:我在实际的调试过程中,没有设置wakeLock.setReferenceCounted(false);这个,报错为:java.lang.RuntimeException: WakeLock under-locked
阅读源码:
acquire()函数如下:
public void acquire()
{
synchronized (mToken) {
if (!mRefCounted || mCount++ == 0) {
try {
mService.acquireWakeLock(mFlags, mToken, mTag);
} catch (RemoteException e) {
}
mHeld = true;
}
}
}
release()函数如下:
public void release()
{
release(0);
}
public void release(int flags)
{
synchronized (mToken) {
if (!mRefCounted || --mCount == 0) {
try {
mService.releaseWakeLock(mToken, flags);
} catch (RemoteException e) {
}
mHeld = false;
}
if (mCount < 0) {
throw new RuntimeException("WakeLock under-locked " + mTag);
}
}
}
报错就抱在release(int flags)中,mCount为负数了,抛除了异常
我们再看下:setReferenceCounted(boolean flags);
public void setReferenceCounted(boolean value)
{
mRefCounted = value;
}
这个函数的作用是是不是需要计算锁的数量,设置为false时,在release()的时候,不管你acquire()了多少回,可以releaseWakeLock掉
11、不让图片变形充满整个屏幕
imageView.setAdjustViewBounds(true);//不让图片充满全局
12、设背景色为透明
getWindow().setBackgroundDrawable(new ColorDrawable(0));
13、中文排版不整齐,一个match_parent解决问题。