How to adapt an Android app to various screen sizes?

1

For example ... I want the Android app to be displayed equally on both 5 "and 3" screens ... And how do I apply a "scroll" layout, how to add a scroll to the layout on Android?

    
asked by anonymous 09.03.2015 / 17:34

2 answers

2

Use wrap_content and match_parent :

To ensure that your layout is flexible and suits different screen sizes, you must use "wrap_content" and "match_parent" for the width and height of some display components. If you use wrap_content, the width or height of the view will be set to the minimum size needed to auto fit to the content inside, while match_parent (also known as fill_parent prior to API 8) causes the component expands to match the size of the view (parent or parent).

Example of using "wrap_content" and "match_parent":

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout android:layout_width="match_parent" 
                  android:id="@+id/linearLayout1"  
                  android:gravity="center"
                  android:layout_height="50dp">
        <ImageView android:id="@+id/imageView1" 
                   android:layout_height="wrap_content"
                   android:layout_width="wrap_content"
                   android:src="@drawable/logo"
                   android:paddingRight="30dp"
                   android:layout_gravity="left"
                   android:layout_weight="0" />
        <View android:layout_height="wrap_content" 
              android:id="@+id/view1"
              android:layout_width="wrap_content"
              android:layout_weight="1" />
        <Button android:id="@+id/categorybutton"
                android:background="@drawable/button_bg"
                android:layout_height="match_parent"
                android:layout_weight="0"
                android:layout_width="120dp"
                style="@style/CategoryButtonStyle"/>
    </LinearLayout>

    <fragment android:id="@+id/headlines" 
              android:layout_height="fill_parent"
              android:name="com.example.android.newsreader.HeadlinesFragment"
              android:layout_width="match_parent" />
</LinearLayout> 

Outcome:

Read more ...

    
12.03.2015 / 00:16
1

I believe that in most cases declaring XML elements using wrap_content and match_parent works fine.

However, to handle different situations with greater freedom, Android allows the declaration of elements based on screen size, density and orientation. This can certainly make all the difference.

To use multiple different files, simply declare them with an identifier. For example: if you declare res/layout/main_activity.xml this feature will be the default, but when you include the res/layout-sw600dp/main_activity.xml file, the system will load this layout if the smallest width is 600dp.

This principle holds for any other qualifier, such as res/layout-land/main_activity.xml that will be loaded if the machine is in landscape mode.

A list of qualifiers can be found here .

    
15.06.2016 / 20:54