杂记android
来源:互联网 发布:ks线切割编程系统 编辑:程序博客网 时间:2024/05/21 11:08
1、生命周期重新加载
<application android:name=".MyApplication" android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar"> <activity android:name=".MainActivity" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|fontScale" android:label="@string/app_name" android:launchMode="singleTask" android:screenOrientation="portrait" android:windowSoftInputMode="adjustPan"> </activity> </application>
在activity加上android:configChanges=”……”属性,就不会重启activity.而只是调用onConfigurationChanged(Configuration newConfig)。
2、嵌套ListView显示不全
public void setListViewHeight(ListView listView) { if (listView == null) { return; } // 得到适配器对象 ListAdapter adapter = listView.getAdapter(); if (adapter == null) { return; } int totalHeight = 0; for (int i = 0; i < adapter.getCount(); i++) { View listItem = adapter.getView(i, null, listView); listItem.measure(0, 0); // 忽略父控件的布局,获取此view的自身的实际大小 totalHeight += listItem.getMeasuredHeight(); } // 分割线的高度 int dividerHeight = listView.getDividerHeight() * (adapter.getCount() - 1); totalHeight += dividerHeight; // 得到父布局的参数 LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) listView.getLayoutParams(); // 重置父布局的高度 params.height = totalHeight; // 设置父布局的参数 listView.setLayoutParams(params); }
3、Parcelable
序列化,表示将一个对象转换成可存储或可传输的状态。序列化后的对象可以在网络上进行传输,也可以存储到本地。
public class Person implements Parcelable { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public static final Creator<Person> CREATOR = new Creator<Person>() { @Override public Person createFromParcel(Parcel in) { Person person = new Person(); person.name = in.readString(); person.age = in.readInt(); return person; } @Override public Person[] newArray(int size) { return new Person[size]; } }; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(name); dest.writeInt(age); }}
4、网络连接状态
/** * ネットワーク接続状態を確認する。<br> * @param context : Application Context * @return 0 : 切断中(圏外)、1 : WiFi接続中、2 : 3G接続中 */ private static final int NETWORK_NONE = 0; // 切断中(圏外) private static final int NETWORK_WIFI = 1; // WiFi接続中 private static final int NETWORK_MOBILE = 2; // 3G接続中 or WiMAX接続中 public static int checkNetworkState(Context context){ int result = NETWORK_NONE; ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = cm.getActiveNetworkInfo(); if( ni != null ){ if (ni.isConnected()) { if (ni.getType() == ConnectivityManager.TYPE_WIFI) { result = NETWORK_WIFI; } else if ((ni.getType() == ConnectivityManager.TYPE_MOBILE) || (ni.getType() == ConnectivityManager.TYPE_WIMAX)) { result = NETWORK_MOBILE; } } } return result; }
5、PopupWindow
private void showPopupWindow() { View contentView = LayoutInflater.from(TextActivity.this).inflate(R.layout.layout_popupwindow_style01, null); PopupWindow popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, false); popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000)); // 设置一个背景 //popupWindow.setBackgroundDrawable(new AnimationDrawable()); popupWindow.setOutsideTouchable(true); // 内容区域外的区域是否响应点击事件 popupWindow.setFocusable(true); // 获得焦点 View rootview = LayoutInflater.from(TextActivity.this).inflate(R.layout.activity_text, null); popupWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0); // 相对于父控件的相对位置 //popupWindow.showAsDropDown(button); // 相对于视图中某个控件的相对位置 }
6、读取文件内容
private String openFile() { try { // 根目录 File sdDir = Environment.getExternalStorageDirectory(); //判断sd卡是否存在 boolean sdCardExist = Environment.getExternalStorageState() .equals(android.os.Environment.MEDIA_MOUNTED); if (!sdCardExist) { return "sd卡未安装"; } /***方法1****/ // 得到字节流 FileInputStream inputStream = new FileInputStream(sdDir + "/uu.txt"); // 将字节流输出为字符流,并指定字符集 InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8"); // 将字符流以缓存的形式一行一行输出 BufferedReader reader = new BufferedReader(inputStreamReader); StringBuilder result = new StringBuilder(); String len = ""; // readLine读取一个文本行 while ((len = reader.readLine()) != null) { result.append(len); } /***方法1****/ /***方法2****/ // 得到字节流 //FileInputStream inputStream = new FileInputStream(sdDir + "/uu.txt"); //byte[] bytes = new byte[6]; //int len; //StringBuilder result = new StringBuilder(); // read(b)从输入流中读取一定数量的字节,并将其存储在缓冲区数组b中 //while ((len = inputStream.read(bytes)) != -1) { // result.append(new String(bytes, 0 ,len)); //} /***方法2****/ Toast.makeText(this,result,Toast.LENGTH_LONG).show(); return result.toString(); } catch (Exception e) { e.printStackTrace(); } return null; }
7、ViewPager
public class MainActivity extends Activity { private View view1, view2, view3; private ViewPager viewPager; //对应的viewPager private List<View> viewList; //view数组 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager = (ViewPager) findViewById(R.id.viewpager); LayoutInflater inflater=getLayoutInflater(); view1 = inflater.inflate(R.layout.layout1, null); view2 = inflater.inflate(R.layout.layout2,null); view3 = inflater.inflate(R.layout.layout3, null); viewList = new ArrayList<View>();// 将要分页显示的View装入数组中 viewList.add(view1); viewList.add(view2); viewList.add(view3); PagerAdapter pagerAdapter = new PagerAdapter() { @Override public boolean isViewFromObject(View arg0, Object arg1) { // 判断是否是同一个View return arg0 == arg1; } @Override public int getCount() { // 返回当前有效视图的个数 return viewList.size(); } @Override public void destroyItem(ViewGroup container, int position, Object object) { // 该方法实现的功能是移除一个给定位置的页面 container.removeView(viewList.get(position)); } @Override public Object instantiateItem(ViewGroup container, int position) { // 这个函数的实现的功能是创建指定位置的页面视图 container.addView(viewList.get(position)); return viewList.get(position); } }; viewPager.setAdapter(pagerAdapter); }}
8、跳转3方app
①、通过包名
Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);intent.setAction(Intent.ACTION_MAIN);intent.addCategory(Intent.CATEGORY_LAUNCHER);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);context.startActivity(intent);
②、通过包名和类名
intent = new Intent();intent.setClassName(packageName, className);intent.setAction(Intent.ACTION_MAIN);intent.addCategory(Intent.CATEGORY_LAUNCHER);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);context.startActivity(intent);
③、通过uri
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));startActivity(intent);
9、六大界面布局方式
1、LinearLayout(线性布局)
最常用的布局,分为垂直布局和线性布局,当垂直布局时,每一行只有一个元素,多个元素依次垂直向下排列;水平布局时,只有一行,每个元素依次向右排列。
2、RelativeLayout(相对布局)
非常常用的布局,相对布局是一种以某一元素(组件或父容器)为参照物,来确定其他组件的布局方式。
3、FrameLayout(帧布局)
组件都是从左上角开始放置,后面会覆盖前面的组件,布局比较简单,只能放一些简单的东西。
4、AbsoluteLayout(绝对布局)
就是用X,Y坐标来指定组件位置,这种坐标适应性太差,很少使用。
5、TableLayout(表格布局)
类似Html的Table,用TableRow来代表表格布局的每一行。
6、Android 4.0新增的网格布局GridLayout
GridLayout与之前的TableLayout有些相似,它把整个容器划分为rows × columns个网格,每个网格可以放置一个组件。性能及功能都要比tablelayout好,比如GridLayout布局中的单元格可以跨越多行,而tablelayout则不行,此外,其渲染速度也比tablelayout要快。
10、ANR
ANR:Application Not Responding
产生原因:在Android中,活动管理器和窗口管理器这两个系统服务负责监视应用程序的响应,当用户的操作在5s内应用程序没做出反应,BroadcastReceiver在10秒内没有执行完毕,就会出现应用程序无响应对话框。
解决方式:Actiity应该在它关键生命周期方法里尽可能去创建操作,潜在耗时操作(网络或数据库操作),或者高耗时操作(改变位图位置等),应该在子线程里(或者异步方式)来完成。
11、获取手机已安装app的包名等
private void getAllVersionCode() { PackageManager packageManager = getPackageManager(); List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0); for (PackageInfo info : packageInfos) { String versionName1 = info.versionName; int versionCode1 = info.versionCode; String packageName1 = info.packageName; Log.d("chg", "--versionName-->" + versionName1); Log.d("chg", "--versionCode-->" + versionCode1); Log.d("chg", "--packageName1-->" + packageName1); } }
- Android杂记
- android 杂记
- android 杂记
- Android杂记
- Android 杂记
- Android杂记
- android 杂记
- android杂记
- Android 杂记
- android杂记
- 杂记android
- ubuntu 与 android 杂记
- Android 开发杂记
- Android AC97驱动杂记
- Android配置杂记
- Android低级错误杂记
- android学习-----杂记一
- Android GPS杂记(1)
- Tab选项卡切换
- 获取客户端的真实IP地址
- 最近公共祖先(LCA)问题 笔记
- B树和B+树
- python基础——装饰器
- 杂记android
- 通过Nvram读取WIFI MAC地址
- Windows下配置Git服务器和客户端及初始使用
- Node.js 中间层 遍历请求接口遇到的问题
- nodeJs常用util工具函数
- 机器学习-softmax逻辑回归源码实现
- 6008
- webpack的基本配置项详解
- java基础知识总结1.0