andoid学习笔记
安卓主流分辨率:
layout-320X240, 480X320, 800X480, 854X480, 960X640, ?1024X600 .为简单起见,Android将屏幕尺寸(屏幕从左上角到右下角的对角线长度)分为四个级别:小,正常,大和超大,?将屏幕密度分成四个基本密度:lDPI(低),mDPI(中等),hDPI(高)以及 xhDPI(特高).?基线”布局设置在320×480(HVGA)上,?一半大小的VGA,?是目前智能手机的标准显示大小。iPhone 3GS,3G和2G都使用这一相同的配置。
android:padding:
padding是站在父view的角度描述问题,它规定它里面的内容必须与这个父view边界的距离。margin则是站在自己的角度描述问题,规定自己和其他(上下左右)的view之间的距离.例如设置EditText中的文字距离边框的填充,就可以使用padding,设置这个EditText在父容器中的位置可以用margin。
android:singleLine="true" :
设置单行显示。经常与android:maxWidth配合使用,设置文本的最大宽度,超出部分以...的形式显示,如:android:text="test_ singleLine "android:singleLine="true"android:maxWidth="60.0dp"android:layout_height="wrap_content"android:layout_width="wrap_content"如果不设置singleLine或者设置为false,文本将自动换行。
android:hint ="@string/..." :
添加灰色提示,有输入时自动消除. 例如:登录EditText框中灰色的"请输入账号..",获得焦点即清空。
android:gravity
意思是这个控件自己的“重力”,在通俗点就是控件上面的东西的位置(图片,文本等)例如:控件内文字置于左下: android:gravity=" left| bottom "
android:layout_gravity:类似android:gravity,但指的是该控件相对于父view的位置。
RelativeLayout相对布局中常用属性:?①指定控件在容器中的相对位置,包括:android:layout_alignParentTop, android:layout_alignParentBottom,android:layout_alignParentLeft,android:layout_alignParentRight,android:layout_centerHorizontal, android:layout_centerVertical, android:layout_centerInParent,他们的值是false|true。配合使用android:layout_marginBottom、Top、Left、Right 可以精确的将控件拜访在靠近容器某个边的位置。eg:
<ProgressBarandroid:id="@+id/loading"android:visibility="invisible"android:layout_width="50.0dip" android:layout_height="50.0dip"android:layout_alignParentBottom="true"android:layout_marginBottom="125.0dip" android:layout_centerHorizontal="true" style="@style/progressloadingstyle"/>?ProcessBar将水平居中显示在距离容器底部125dip的位置,如果不加layout_alignParentBottom="true",android:layout_marginBottom不起作用。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/view" android:background="@drawable/background_login" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/text1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:textColor="#000000"android:text="TextView1"/><TextView android:id="@+id/text2"android:layout_below="@id/text1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#000000"android:text="TextView2"/><EditTextandroid:id="@+id/edit1"android:layout_toRightOf="@id/text2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignTop="@id/text2"/> </RelativeLayout>?效果图:
processDialog = new ProgressDialog(Context context);processDialog.setMessage(String message);processDialog.setIndeterminate(false);processDialog.setCancelable(true);processDialog.setOnCancelListener(mOnCancelListener);processDialog.show();ProgressBar的setIndeterminate(false);设为false,表示滚动条的当前值自动在最小到最大值之间来回移动,形成这样一个动画效果,这个只是告诉别人"我正在工作",但不能提示工作进度到哪个阶段。主要是在进行一些无法确定操作时间的任务时作为提示。设为true,就是根据你的进度可以设置现在的进度值。
ActivityManager am = (ActivityManager)getSystemService (Context.ACTIVITY_SERVICE); am.restartPackage(getPackageName());? ?这个方法,系统会将该包下的所有 进程、服务,全部杀掉,要注意清单文件:
Intent startMain = new Intent(Intent.ACTION_MAIN);startMain.addCategory(Intent.CATEGORY_HOME); startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);startActivity(startMain);System.exit(0);//退出程序???
<activity android:name=".EX01" android:label="@string/app_name" android:screenOrientation="portrait">// 竖屏 , 值为 landscape 时为横屏………… </activity>
private int m_id;private int m_position;mlistView.setOnItemLongClickListener(new OnItemLongClickListener() {@Overridepublic boolean onItemLongClick(AdapterView<?> arg0, View v,int position, long id) {m_id = id;m_position = position;return false; //交给onContextItemSelected继续做}});@Overridepublic boolean onContextItemSelected(MenuItem item) {if(item.getItemId() == 0){//打开Intent intent = new Intent(Myself.this, SubList.class);intent.putExtra("mid", m_id);String m_name = myObjectList.get(m_position-1).getName(); intent.putExtra("mname", m_name);startActivity(intent);}else if(item.getItemId() == 1){//删除}return super.onContextItemSelected(item);}@Overridepublic void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); //menu.setHeaderIcon(R.drawable.icon); // 创建上下文菜单 menu.setHeaderTitle("请选择操作"); //menu.clearHeader(); //清除表头 // 第一个参数:组ID // 第二个参数:菜单项ID // 第三个参数:顺序号 // 第四个参数:菜单项上显示的内容 menu.add(1,0,0,"打开"); menu.add(1,1,1,"删除");}?代码中如果setOnItemLongClickListener返回了true,onCreateContextMenu方法就不会执行,长按listview的某个item也就不会构建并弹出上下文菜单。
<meta name=”viewport” content=”width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0″>?width表示网页的布局layout宽度。initial-scale表示初始时的缩放比例,minimum-scale和maximum-scale分别表示最小和最大缩放比例。width只是设置layout宽度,还要乘上缩放比例,才能得到最终的显示宽度。那么对于480像素的屏幕来说,若device-width“被320”,那initial-scale应该是1.5才能占满整个屏幕宽度。
android:scaleType="centerInside"android:adjustViewBounds="true"注意:控制的图片为资源而不是背景,即android:src。关于ImageView的更多属性,参考:http://www.cnblogs.com/over140/archive/2011/06/08/2075054.html
FrameLayout fr=(FrameLayout)findViewById(R.id.FrameLayout01); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/test.jpg", options); //此时返回bm为空 options.inJustDecodeBounds = false; //缩放比 int be = (int)(options.outHeight / (float)200); if (be <= 0) be = 1; options.inSampleSize = be; //重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false哦 bitmap=BitmapFactory.decodeFile("/sdcard/test.jpg",options); int w = bitmap.getWidth(); int h = bitmap.getHeight(); System.out.println(w+" "+h); ImageView iv=new ImageView(this); iv.setImageBitmap(bitmap);这样我们就可以读取较大的图片而不会内存溢出了。如果你想把压缩后的图片保存在Sdcard上的话就很简单了:File file=new File("/sdcard/feng.png"); try { FileOutputStream out=new FileOutputStream(file); if(bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)){ out.flush(); out.close(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }?压缩图片质量:?
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fos); 其中的quality为0~100, 可以压缩图片质量, 不过对于大图必须对图片resize 这个是等比例缩放: bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false); 这个是截取图片某部分: bitmap = Bitmap.createBitmap(bitmap, x, y, width, height);?