Responsive design for all android devices

北城以北 提交于 2019-12-08 16:27:16

问题


I am using two different methods to achieve responsive design in android devices

Method 1. I used different dimens.xml in different folders like

values-hdpi
values-ldpi
values-mdpi
values-xhdpi  
values-xxhdpi

Method 2. I copied all my layouts to

layout
layout-large
layout-small
layout-xlarge

folders and given different heights, widths and other

When I use the first method I am not getting the correct responsive design and when I use the second method I am getting the correct design but it increasing the application size.

So, please tell me the best process to achieve 100% responsive design other than my two methods


回答1:


I usually go for the values, values-large, ... folders and put dimens.xml files there, where I specify the sizes for each screen size category. The values is the "fallback", it contains the default value if I don't specify any for a specific size category. In the layouts I use it like this:

android:width="@dimen/width_for_this_view"

Defined in the values/dimens.xml:

<dimen name="width_for_this_view">30dp</dimen>

You can define different sizes for different values (large, normal, small, ..) folder.




回答2:


You can use google library PercentRelativeLayout with this library you can set width, height and margin of your views by percentage which is great because in all screen they look the same and of course it is not hard to code it. Here example:

<android.support.percent.PercentRelativeLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
     <ImageView
         app:layout_widthPercent="50%"
         app:layout_heightPercent="50%"
         app:layout_marginTopPercent="25%"
         app:layout_marginLeftPercent="25%"/>
 </android.support.percent.PercentRelativeLayout>

you must add this line in your build.gradle

dependencies {
    compile 'com.android.support:percent:23.2.0'
}

Official documentation by Google https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html

Hope this help for your case!



来源:https://stackoverflow.com/questions/36264377/responsive-design-for-all-android-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!