Android 编程 技巧

  1. Add Two Numbers: Your first non-tutorial application should be to take two numbers and add them together. It sounds too simple. You will spend some hours getting the layouts, the callbacks, and onPause/onResume to work correctly. Do it.
  2. It is Java: You work in Java for most of your Android programming. Don't spend time praising it. Don't spend time complaining about it. Just work with it.
  3. Love RelativeLayout: Most of the tutorials use LinearLayout, but you will find that RelativeLayout is truly useful. Many layouts, like the GridLayout aren't used much at all. Play around with RelativeLayout. See an example from this question.
  4. Use fill_parent with a top level RelativeLayout: A surprisingly common and hard to find problem is putting a wrap_content in a top level RelativeLayout and then wondering why unrelated fields far down in the hierarchy are rendering strangely.
  5. Use empty layout items: You will often use empty items in your layouts just for positioning other layouts. For example, you might use an empty TextField, of width=0 and height=0 and centerInParent='True' just to anchor things relative to the middle of the screen. Also, you might have an empty TextField or LinearLayout so that you can give a layout_weight=1 to it and have it take up more screen space.
  6. Set a layout background color: If you are having trouble figuring out your layout, try setting the background colors on some objects. It can highlight your mistakes faster than other tools, and shows some surprises that the IDE red box doesn't always help.
  7. Download Apps-For-Android: This is a big chunk of useful source code for a half dozen applications. It can supplement the sample applications nicely and show different coding style solutions. Grab it using svn co http://apps-for-android.googlecode.com/svn/trunk/ apps-for-android-read-only
  8. Download the source: You need the Android source to solve some problems or, more likely, get past holes in the documentation. Your copy does not need to perfect or kept up to date. You can learn to use the repo command, or just visit http://bit.ly/biOLUt for snapshot.
  9. Learn to search your source: The fastest solution to many problems is to find where a particular parameter is used in some other source. Put a copy or link to the sample applications, apps-for-android applications, and any other source you have under one directory tree. Use "grep -ir funky_parameter sample_code/" or your favorite searching routine to quickly find some code that uses that parameter.
  10. Use Eclipse: Even you have a favorite editor or IDE you have used for years, use Eclipse for Android development. It is good enough as an IDE and is really part of the development tools suite. Any time you spend trying to jury-rig your IDE to work is time you didn't code.
  11. Learn Eclipse: Learn a few new tricks with Eclipse every day. Some of my favorite commands were found reading this list and this question.
  12. Get help when starting out: The quantity of readable material can be overwhelming. Setting up the environment can be tricky. Going to an Android Meet-up or users group can help get over the initial hump.
  13. Code every day: Android code will be frustrating. Don't allow yourself to stop because you get stuck. Play around with the tools, step through a sample application, read one of the articles, or read a blog to ease the frustration. Then write some more code.
  14. Lurk on IRC: Connect your favorite IRC reader to irc.freenode.net's #android-dev channel. Leave it up in the background. Only ask questions after you have spent ten minutes trying to figure it out on your own.
  15. Use two monitors: Working on your laptop in the coffee shop will slow you down. You need to spread out windows. At very least, you will want a full screen Eclipse session, the emulator, and a browser with the tutorial to all be easily visible. Three screens may work even better.
  16. Reformat XML files: The layout editor makes a hash of the XML files. Use the 'source/format' command to put them in a reasonable form. You will want to check the "Eclipse/Windows/Preferences/XML/XML Files/Editor/Formatting/Split XML attributes each on a new line" check box. Then use shift-ctrl-F to format it.
  17. Edit XML files with the text editor: After the first couple of layout screens, stop using the slow moving 'properties' gui to change properties. Drop the items using the gui. Use the up/down arrows on the far right outline to get the hierarchy of views and layouts correct, then edit the XML file directly and use the ctrl-space shortcut to bring up possible completions and explanations of the properties.
  18. Plan on Piracy in the MarketPlace: Google has not made the MarketPlace a Happy Place. Apps are copied and reposted with changed names to funnel money around. Lots of scammers are trying to game the system in many ways. Don't plan on making a living solely from AppStore revenues nor plan on Google being responsive.
  19. Use LogCat: It can be difficult in Android to figure out 'what went wrong'. Run the application in the debugger and look at the logcat window. I found it worthwhile to add a new perspective with just a maximized logcat window. If you like to have a colored LogCat output in a separate window try this tool: Colored LogCat
  20. Explore the tools directory: There a lot helpful tools in the sdk's tools directory, such as hierarchyviewer and layoutopt. Each is helpful and there is no shortcut to learning about each tool one at a time.


Designing for Performance covers:

  • Optimize Judiciously
  • Avoid Creating Objects
  • Performance Myths
  • Prefer Static Over Virtual
  • Avoid Internal Getters/Setters
  • Use Static Final For Constants
  • Use Enhanced For Loop Syntax
  • Avoid Enums Where You Only Need Ints
  • Use Package Scope with Inner Classes
  • Use Floating-Point Judiciously
  • Know And Use The Libraries
  • Use Native Methods Judiciously

Best Practices for User Interfaces:

1 Read the UI guidelines

2 Understand and design for touch mode

3 But, support multiple interaction modes

4 Use notifications and the window shade

5 Support interaction between applications

6 Keep your UI fast and responsive

7 Use widgets and live folders

8 Handle screen orientation changes

9 Use images wisely

10 Use layouts that adapt to multiple devices


  1. DalvikVM Garbage Collector calls are expensive. They can lockup the UI thread easily. If you want smooth scrolling you should avoid creating too many objects. Prefer the reuse of existing objects and not creating new ones. You can analyze your object creation statistics with the "Allocation Tracker" tab in the Eclipse DDMS perspective.

  2. Starting a new thread is expensive. Use one thread that executes required tasks one by one, or use thread pools. You should use built in classes like AsyncTask whenever possible.

  3. If your data set gets relatively large (>100KB) don't store it in text file or XML file - parsing it will be too expensive. Use the native SQLite implementation instead.

  4. Eclipse MAT is a great tool to analyze memory allocations. It will help to find memory leaks in your application.

  • Close resources (files/streams, ...) after you do not need them any more
  • Override onDestroy() to clean up everything
  • Make use of onPause()/onResume to save or close what does not need to be opened the whole time.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值