1、安卓开发一些基础的设置代码DialogFragment 设置背景透明onCreateViewWindow window = getDialog().getWindow();if (window != null) window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT);简单的 btn 状态选择器Fragment 和 Activity 的交互需要注意的是 getActivity() 有可能会返回 nullView xxx = getActivity().findViewById(R.id.xxx);获得 fragment
2、的引用要用 FragmentManager,之后可以调用 findFragmentById() 或者 findFragmentByTag()ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);事件回调一些情况下,可能需要 fragment 和 activity 共享事件,一个比较好的做法是在 fragment 里面定义一个回调接口,然后要求 activity 实现它,当 activity通过这个接口接收到一个回调,它可以同布局中的其他
3、fragment 分享这个信息。监听 Fragment Back 的两个方法 写回调实现 判断 RootView Override public void onResume() super.onResume(); getView().setFocusableInTouchMode(true); getView().requestFocus(); getView().setOnKeyListener(new View.OnKeyListener() Override public boolean onKey(View v, int keyCode, KeyEvent event) if (eve
4、nt.getAction() = KeyEvent.ACTION_UP return false; ); 调用市场 playUri uri = Uri.parse(“market:/details?id=“ + context.getPackageName();Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);try startActivity(goToMarket); catch (ActivityNotFoundException e) 自定义 View(1 )定义一个 Class 继承于系统 View;(2 )在 xml 中配
5、置 layout 布局文件;(3 )重写父类的一些方法,如 onMeasure、onDraw、onLayout 等;(4 )在程序中应用自定义的 View;onMeasure()用来设置视图的大小,即视图的宽度和高度onLayout()用于设置视图在屏幕中显示的位置onDraw()利用前面两部分得到的参数,将视图显示在屏幕上android 的设计模式模版模式每次新建一个 Activity 时都会覆盖 onCreate(),onStart()方法等,这些方法在父类中就相当于一个模板观察者模式点击事件适配器模式adapter单例模式Application工厂模式BitmapFactory.deco
6、deResource();BitmapFactory 相当于位图工厂代理模式AIDLLayout 属性android:clipToPadding:配合 paddingTop 可简单实现 View 距离顶部一定距离android:clipChildren:是否限制子 View 在其范围内调用显示触摸位置功能android.provider.Settings.System.putInt(getContentResolver(), “show_touches“, 1);代码切换全屏/切换到全屏 getWindow().clearFlags(WindowManager.LayoutParams.FLA
7、G_FORCE_NOT_FULLSCREEN);getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);/切换到非全屏getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);Activity 透明android:color/transparentnulltruetruenull展开、收起状态栏pu
8、blic static final void collapseStatusBar(Context ctx) Object sbservice = ctx.getSystemService(“statusbar“);try Class statusBarManager = Class.forName(“android.app.StatusBarManager“);Method collapse;if (Build.VERSION.SDK_INT = Build.VERSION_CODES.JELLY_BEAN_MR1) collapse = statusBarManager.getMethod(
9、“collapsePanels“); else collapse = statusBarManager.getMethod(“collapse“);collapse.invoke(sbservice); catch (Exception e) e.printStackTrace();public static final void expandStatusBar(Context ctx) Object sbservice = ctx.getSystemService(“statusbar“);try Class statusBarManager = Class.forName(“android
10、.app.StatusBarManager“);Method expand;if (Build.VERSION.SDK_INT = 17) expand = statusBarManager.getMethod(“expandNotificationsPanel“); else expand = statusBarManager.getMethod(“expand“);expand.invoke(sbservice); catch (Exception e) e.printStackTrace();获取网络类型名称public static String getNetworkTypeName(
11、Context context) if (context != null) ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);if (connectMgr != null) NetworkInfo info = connectMgr.getActiveNetworkInfo();if (info != null) switch (info.getType() case ConnectivityManager.TYPE_WIFI
12、:return “WIFI“;case ConnectivityManager.TYPE_MOBILE:return getNetworkTypeName(info.getSubtype();return getNetworkTypeName(TelephonyManager.NETWORK_TYPE_UNKNOWN);public static String getNetworkTypeName(int type) switch (type) case TelephonyManager.NETWORK_TYPE_GPRS:return “GPRS“;case TelephonyManager
13、.NETWORK_TYPE_EDGE:return “EDGE“;case TelephonyManager.NETWORK_TYPE_UMTS:return “UMTS“;case TelephonyManager.NETWORK_TYPE_HSDPA:return “HSDPA“;case TelephonyManager.NETWORK_TYPE_HSUPA:return “HSUPA“;case TelephonyManager.NETWORK_TYPE_HSPA:return “HSPA“;case TelephonyManager.NETWORK_TYPE_CDMA:return
14、“CDMA“;case TelephonyManager.NETWORK_TYPE_EVDO_0:return “CDMA - EvDo rev. 0“;case TelephonyManager.NETWORK_TYPE_EVDO_A:return “CDMA - EvDo rev. A“;case TelephonyManager.NETWORK_TYPE_EVDO_B:return “CDMA - EvDo rev. B“;case TelephonyManager.NETWORK_TYPE_1xRTT:return “CDMA - 1xRTT“;case TelephonyManage
15、r.NETWORK_TYPE_LTE:return “LTE“;case TelephonyManager.NETWORK_TYPE_EHRPD:return “CDMA - eHRPD“;case TelephonyManager.NETWORK_TYPE_IDEN:return “iDEN“;case TelephonyManager.NETWORK_TYPE_HSPAP:return “HSPA+“;default:return “UNKNOWN“;扫描指定的文件sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
16、 uri);有没有应用程序处理你发出的 intentpublic static boolean isIntentAvailable(Context context, String action) final PackageManager packageManager = context.getPackageManager();final Intent intent = new Intent(action);List list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);ret
17、urn list.size() 0;TransitionDrawable 实现渐变效果private void setImageBitmap(ImageView imageView, Bitmap bitmap) final TransitionDrawable td = new TransitionDrawable(new Drawablenew ColorDrawable(R.color.colorAccent), new BitmapDrawable(imageView.getContext().getResources(), bitmap);imageView.setBackgroundDrawable(imageView.getDrawable();imageView.setImageDrawable(td);td.startTransition(200);