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(/blog_article/url/index.html);
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"
如下图我想一次添加多个 seekbar,但是family呢只出现一个,怎么才能实现第一章图的情况呢。
mymain.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:scrollbars="vertical"> <LinearLayout android:orientation="vertical" android:id="@+id/myMainLayout" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Family" android:id="@+id/Family" android:textSize="16px" android:padding="5px" android:text android:gravity="center_horizontal"></TextView> </LinearLayout> </ScrollView>
另一个 xml (myviews.xml),
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text android:id="@+id/Module" android:text="Location"> </TextView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="context_notion_level_off" android:layout_alignRight="@+id/Module" android:id="@+id/ContextNotionLevel"> </TextView> <SeekBar android:layout_height="wrap_content" android:id="@+id/SeekBar01" android:layout_width="fill_parent" android:padding="5px" android:saveEnabled="true" android:layout_below="@+id/Module" android:max="2"> </SeekBar> </RelativeLayout>
super.onCreate(savedInstanceState); setContentView(R.layout.mymain); LinearLayout l = (LinearLayout) findViewById(R.id.myMainLayout); LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); for(int i = 0 ; i < 15; i++) { View myView = linflater.inflate(R.layout.myviews, null); SeekBar s = (SeekBar) myView.findViewById(R.id.SeekBar01); //表明哪一个被使用 s.setId(i); TextView t = (TextView) myView.findViewById(R.id.Module); // 动态修改文字 t.setText(("My location " + i).toString()); s.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){ @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { Log.i("=========","My SeekBar = " + seekBar.getId()); Log.i("=========","My Progress = " + progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub }}); l.addView(myView);
Bitmap 相关
1. Bitmap比较特别 因为其不可创建 而只能借助于BitmapFactory 而根据图像来源又可分以下几种情况:
* png图片 如:R.drawable.tianjin
Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.tianjin);
* 图像文件 如: /sdcard/dcim/tianjin.jpeg
Bitmap bmp = BitmapFactory.decodeFile("/sdcard/dcoim/tianjin.jpeg")
2. Bitmap 相关应用
- 本地保存 即 把 Bitmap 保存在sdcard中
* 创建目标文件的File
File fImage = new File("/sdcard/dcim","beijing.jpeg"); FileOutputStream iStream = new FileOutputStream(fImage);
* 取出Bitmap oriBmp
oriBmp.compress(CompressFormat.JPEG, 100, iStream);
- 得到网路图片
* 定义网络图片对应的BufferedInputStream
//图片的链接地址 String icoURI = "http://202.140.96.134:8080/FS-RSS/img/RN.png"; URL imgURL = new URL(/blog_article/iu/index.html); URLConnection conn = imgURL.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is);
* 下载之
Bitmap bmp = BitmapFactory.decodeStream(bis);
* 关闭Stream
bis.close(); is.close();
这个方法貌似不能解析jpg类型的图片。不知楼主试过没。
这个方法貌似不能解析jpg类型的图片。不知楼主试过没。
是的 *.jpg解析可以通过BitmapFactory.decodeFile("/sdcard/dcim/test.jpg")