Can we create one type of layout for all screen sizes?

I developed applications that use several types of layouts for different types of screens and resolutions, but I noticed that some developers use only one type of layout, which one is better to use for a single or multiple layout type?

Several types of layout, for example

layout-large-mdpi   
layout-large-tvdpi  
layout-large-xhdpi 
layout-xlarge-mdpi  
layout-xlarge-xhdpi 
-1
source share
3 answers

Using different file sizes, you can do this.

values-mdpi → dimensions.xml

values-hdpi → dens.xml

values-xhdpi → dens.xml

values-xxhdpi → dens.xml

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

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

and for values-hdpi -> dens.xml

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

-xhdpi → dimens.xml

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

dimen

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

.

+2

. .

//first create layout xml in layout folder in my case it 
//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>

. , . , .

. ( ), ( ), - ( ), -xlarge ( ). dimens.xml .

, /dimens.xml .

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

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

</resources>

-large/dimens.xml

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

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

 </resources>

- small/dimens.xml

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

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

  </resources>

- xlarge/dimens.xml

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

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

  </resources>
0

No, the application can adapt to different screen sizes, but you must create different layouts for different sizes.

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

0
source

Source: https://habr.com/ru/post/1690661/


All Articles