How To Handle Android Screens Fragmentations?
Solution 1:
You should only be using completely different layouts for large changes in how you want your app to behave, not for minor variances in screen size within a single device class such as phones. You might provide a -land
variant of your layouts that are optimized for landscape mode, or you might provide a -sw600dp
variant to present a different information architecture for tablets of around 7" or larger. You should not use this to provide different layouts for a WVGA vs. a qHD phone display. Doing so will only create a lot of extra work for yourself.
dp is a tool for working with different densities, it will not help for variances in size and aspect ratio. The solution for working with variance in screen size and aspect ratio is to use the layout managers provided in the framework to adapt to the current device. You can quite easily write your own by extending ViewGroup
and overriding the onMeasure
and onLayout
methods if you come to a situation where you truly need custom behavior. API 14 added GridLayout
, which offers further options as well.
Designing for these variances does require a different approach to the problem. You cannot approach the problem by thinking in absolute screen coordinates unless you want to make a lot of work for yourself. Think about how the components in your layout fit together relative to one another, and which pieces should expand to consume more space than your minimum configuration when it is available.
Thinking in terms of percentages is a good start and that's exactly the kind of results you'll get by using LinearLayout
's weight feature. But if you want to specify that a certain view should maintain a specific aspect ratio you will need to think about how that should be bounded, what will fill any excess space, and whether or not those decisions will produce a good user experience for your app. (For example, no user wants to use an app that letterboxes itself and only shows real content in a fixed aspect ratio box in the middle.)
There is no magic solution to this since only you know how these things should behave in your app. Android will help you but you have to tell it what you want.
Perhaps you could post a question about a specific design that you are having trouble with and someone here on stackoverflow could offer some suggestions.
Solution 2:
my way to draw custom views with the right scale for each screen size is to get the actual screen density:
float density = context.getResources().getDisplayMetrics().density;
and for example setting a font size for a custom view with 16 density independent pixels
paint.setTextSize(density * 16);
Thats the whole magic. I hope this will solve your problem.
Post a Comment for "How To Handle Android Screens Fragmentations?"