Currently, there are too many android models on the market. It is very realistic and necessary to solve the problem of how to adapt your applications to different models. The solution documentation officially provided by android is analyzed and processed in three directions.
(1) Support devices of Different Screen Sizes (Supporting Different Screen Sizes)
1.1 reasonably use wrap_content, match_parent
1.2 try to use RelativeLayout
1.3 Use different layout files (Use Size Qualifiers) for different models ):
Res/layout/main. xml
Res/layout-large/main. xml
1.4 try to Use the image (Use Nine-patch Bitmaps)
(2) Supporting Different Screen Densities)
2.1 Use Density-independent phase units (Use Density-independent Pixels )-----Dp, sp
2.2 Provide selectable images (Provide Alternative Bitmaps) for different devices)
Proportional Relationship between different resolutions:
Xhdpi: 2.0
Hdpi: 1, 1.5
Mdpi: 1.0 (baseline)
Ldpi: 0.75
Awesomeimage.png placement:
MyProject/
Res/
Drawable-xhdpi/
Awesomeimage.png
Drawable-hdpi/
Awesomeimage.png
Drawable-mdpi/
Awesomeimage.png
Drawable-ldpi/
Awesomeimage.png
(3) Implementing an adaptive User Interface stream (Implementing Adaptative UI Flows)
3.1 identify the Current Layout file (Determine the Current Layout): This mainly uses the flag to obtain the Layout file currently in use.
public class NewsReaderActivity extends FragmentActivity { boolean mIsDualPane; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); View articleView = findViewById(R.id.article); mIsDualPane = articleView != null && articleView.getVisibility() == View.VISIBLE; }}
For controls (eg: button) not available in the current layout file, you need to determine whether it is null:
Button catButton = (Button) findViewById(R.id.categorybutton);OnClickListener listener = /* create your listener here */;if (catButton != null) { catButton.setOnClickListener(listener);}
3.2 use different response operations (React According to Current Layout) based on the Current Layout)
public void onHeadlineSelected(int index) { mArtIndex = index; if (mIsDualPane) { /* display article on the right pane */ mArticleFragment.displayArticle(mCurrentCat.getArticle(index)); } else { /* start a separate activity */ Intent intent = new Intent(this, ArticleActivity.class); intent.putExtra("catIndex", mCatIndex); intent.putExtra("artIndex", index); startActivity(intent); }}
final String CATEGORIES[] = { "Top Stories", "Politics", "Economy", "Technology" };public void onCreate(Bundle savedInstanceState) { .... if (mIsDualPane) { /* use tabs for navigation */ actionBar.setNavigationMode(android.app.ActionBar.NAVIGATION_MODE_TABS); int i; for (i = 0; i < CATEGORIES.length; i++) { actionBar.addTab(actionBar.newTab().setText( CATEGORIES[i]).setTabListener(handler)); } actionBar.setSelectedNavigationItem(selTab); } else { /* use list navigation (spinner) */ actionBar.setNavigationMode(android.app.ActionBar.NAVIGATION_MODE_LIST); SpinnerAdapter adap = new ArrayAdapter(this, R.layout.headline_item, CATEGORIES); actionBar.setListNavigationCallbacks(adap, handler); }}
3.3 Reuse Fragments (Reuse Fragments in Other Activities) in Other Activities ):
In this section, android provides a solution to solve the same operation and different responses. It provides an interface in the parent class and implements the interface in the subclass to implement different operations:
public class HeadlinesFragment extends ListFragment { ... OnHeadlineSelectedListener mHeadlineSelectedListener = null; /* Must be implemented by host activity */ public interface OnHeadlineSelectedListener { public void onHeadlineSelected(int index); } ... public void setOnHeadlineSelectedListener(OnHeadlineSelectedListener listener) { mHeadlineSelectedListener = listener; }}
public class HeadlinesFragment extends ListFragment { ... @Override public void onItemClick(AdapterView
parent, View view, int position, long id) { if (null != mHeadlineSelectedListener) { mHeadlineSelectedListener.onHeadlineSelected(position); } } ...}
Original materials:
1. Designing for Multiple Screens:
Http://developer.android.com/training/multiscreen/index.html
2. Supporting Different Screen Sizes:
Http://developer.android.com/training/multiscreen/screensizes.html
3. Supporting Different Screen Densities:
Http://developer.android.com/training/multiscreen/screendensities.html
4. Implementing Adaptative UI Flows:
Http://developer.android.com/training/multiscreen/adaptui.html
References:
1.
Http://blog.csdn.net/hfreeman2008/article/details/9351687