小常识集锦
1.安排布局的时候不能吧ListView放在 ScrollView里面如:
<ScrollView?
? ? ? ? android:id="@+id/widget29"?
? ? ? ? android:layout_width="fill_parent"?
? ? ? ? android:layout_height="fill_parent">?
? ? ? ? <ListView?
? ? ? ? ? ? android:id="@+id/lstView1"?
? ? ? ? ? ? android:layout_width="fill_parent"?
? ? ? ? ? ? android:layout_height="fill_parent">?
? ? ? ? </ListView>?
? ? </ScrollView>?
2.通过getIdentifier获得资源
?private void showImage() {?
? ? String uri = "drawable/icon";?
?
? ? // int imageResource = R.drawable.icon;?
? ? int imageResource = getResources().getIdentifier(uri, null, getPackageName());?
?
? ? ImageView imageView = (ImageView) findViewById(R.id.myImageView);?
? ? Drawable image = getResources().getDrawable(imageResource);?
? ? imageView.setImageDrawable(image);?
}?
?
或者String uri = "@drawable/myresource.png";
一般不推荐这样使用 但是不排除变态要求这样。
?
3. 通过连接打开一个网页
// 当点击一个新 URL
//?默认的 web browser 启动
webView.setWebViewClient(new WebViewClient() {?
? ? @Override?
? ? public boolean shouldOverrideUrlLoading(WebView view, String url) {?
? ? ? ? view.loadUrl(url);?
? ? ? ? return true;?
? ? }?
});?
?
4.获得手机的信息
?
mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);?
String imei = mTelephonyMgr.getDeviceId();?
官方文档:
String getSimCountryIso() : Returns the ISO country code equivalent for the SIM provider's country code.
String getSimOperator() : Returns the MCC+MNC (mobile country code + mobile network code) of the provider of the SIM.
String getSimOperatorName() : Returns the Service Provider Name (SPN).
String getSimSerialNumber() : Returns the serial number of the SIM, if applicable.
int getSimState() : Returns a constant indicating the state of the device SIM card.
String getSubscriberId() : Returns the unique subscriber ID, for example, the IMSI for a GSM phone.
?
5.程序中加入一个网站
button.setOnClickListener(new OnClickListener() {?
? public void onClick(View view) {?
? ? Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://facebook.com"));?
? ? startActivity(intent);?
? }?
});?
6.隐藏输入法
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
?
7.让button在listview下面? 一般用相对布局
<RelativeLayout ?
?android:layout_width="wrap_content" ?
?android:layout_height="wrap_content" ? ? ? ? ?
?android:layout_centerHorizontal="true">?
? <ListView ...>?
? <Button android:id="@+id/btnGetMoreResults"?
? ?android:layout_height="wrap_content" ?
? ?android:layout_width="wrap_content" ? ? ?
? ?android:text="Get more"?
? ?android:layout_alignParentBottom="true" />?
</RelativeLayout>
?
8自动发起一个查看图片的设备
?Uri uri = Uri.fromFile("/blah/myimage.jpg"); ?
?Intent intent = new Intent(android.content.Intent.ACTION_VIEW); ?
?intent.setDataAndType(uri, "image/jpg"); ?
?startActivity(intent);?
9.toast中自定义一些空间
LayoutInflater inflater = getLayoutInflater();?
View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));?
?
TextView text = (TextView) layout.findViewById(R.id.text);?
?
text.setText(content);?
image.setImageBitmap(bmImg);?
?
ImageView image = (ImageView) layout.findViewById(R.id.image);?
Toast toast = new Toast(getApplicationContext());?
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);?
toast.setDuration(Toast.LENGTH_LONG);?
toast.setView(layout);?
toast.show();?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"?
? ? ? ? ? android:id="@+id/toast_layout_root"?
? ? ? ? ? android:orientation="horizontal"?
? ? ? ? ? android:layout_width="fill_parent"?
? ? ? ? ? android:layout_height="fill_parent"?
? ? ? ? ? android:padding="10dp"?
? ? ? ? ? android:background="#DAAA"?
? ? ? ? ? >?
<ImageView android:id="@+id/image"?
? ? ? ? ? ?android:layout_width="40dp"?
? ? ? ? ? ?android:layout_height="40dp"?
? ? ? ? ? ?android:layout_marginRight="10dp"?
? ? ? ? ? ?/>?
<TextView android:id="@+id/text"?
? ? ? ? ? android:layout_width="wrap_content"?
? ? ? ? ? android:layout_height="fill_parent"?
? ? ? ? ? android:textColor="#FFF"?
? ? ? ? ? />?
</LinearLayout>
10.让背景有个圆角 通常是加一个背景 然后引用:
<?xml version="1.0" encoding="UTF-8"?>?
<shape xmlns:android="http://schemas.android.com/apk/res/android">?
? ? <solid android:color="#99FFFFFF"/>?
? ? <corners android:radius="30px"/>?
? ? <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" /> ?
</shape>
android:background="@drawable/my_shape_file"?