Can we make one Layout type for all screen size

∥☆過路亽.° 提交于 2019-12-17 20:42:37

问题


I developed apps using multiple layouts types for different screens types and resolutions but I have notices that some developers using only one layout type,Which one is a better practices to continue with Single or Multiple layout type?

Multiple layout type like

layout-large-mdpi   
layout-large-tvdpi  
layout-large-xhdpi 
layout-xlarge-mdpi  
layout-xlarge-xhdpi 

回答1:


Using different dimens files you can do this thing.

values-mdpi ->dimens.xml

values-hdpi ->dimens.xml

values-xhdpi ->dimens.xml

values-xxhdpi ->dimens.xml

For example: define one in values-mdpi ->dimens.xml

<dimen name="scale_1dp">10dp</dimen>

and for values-hdpi ->dimens.xml

<dimen name="scale_1dp">12dp</dimen>

and for values-xhdpi ->dimens.xml

<dimen name="scale_1dp">15dp</dimen>

And after that use this dimen

<ImageView
     android:layout_width="@dimen/scale_1dp"
     android:layout_height="wrap_content"
     android:layout_gravity="center"/>

same thing for text size.




回答2:


This make scene

Note : Recommended way is different.

//first create layout xml in layout folder in my case it's 
//sample_activity.xml below is the xml code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">

    <Button
        android:layout_width="@dimen/button_width"
        android:layout_height="@dimen/button_height"
        android:layout_centerInParent="true"
        android:text="This is button"/>


</RelativeLayout>

Look in above code I am taking values for Button height and width from dimens file which is in values folder. By default layout folder points to normal screen size and values folder points to normal screen size.

Now we have to create different values folders for different screen size. Like values(for normal), values-large(for large screen), values-small(for small screen size), values-xlarge(for extra large screen size). Create dimens.xml file in all folders.

So first values/dimens.xml file is like below code.

<?xml version="1.0" encoding="utf-8"?>
<resources>

 <dimen name="button_width">100dp</dimen>
 <dimen name="button_height">50dp</dimen>

</resources>

Second values-large/dimens.xml

 <?xml version="1.0" encoding="utf-8"?>
 <resources>

 <dimen name="button_width">200dp</dimen>
 <dimen name="button_height">100dp</dimen>

 </resources>

Third values-small/dimens.xml

  <?xml version="1.0" encoding="utf-8"?>
  <resources>

  <dimen name="button_width">10dp</dimen>
  <dimen name="button_height">50dp</dimen>

  </resources>

Fourth values-xlarge/dimens.xml

  <?xml version="1.0" encoding="utf-8"?>
  <resources>

  <dimen name="button_width">200dp</dimen>
  <dimen name="button_height">100dp</dimen>

  </resources>



回答3:


No, the app can adapt to different screen sizes, but you have to create different layouts for the different sizes.

Then the system will select the correct layout based on the screen size of the device.



来源:https://stackoverflow.com/questions/47524010/can-we-make-one-layout-type-for-all-screen-size

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